OpenExeConfiguration路径问题 [英] OpenExeConfiguration Path Issue

查看:193
本文介绍了OpenExeConfiguration路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器加载web.config。
当我尝试这个时,它位于:\server\folders\web.config



  ConfigurationManager.OpenExeConfiguration(@\\server\folders\web.config); 

它搜索:web.config.config并失败。

  ConfigurationManager.OpenExeConfiguration(@\\server\folders\web); 

它失败,因为没有文件夹\server\folders\web\

所以我试过几个东西,它似乎检查路径指向的文件存在,然后它应用一个.config并获取配置文件。



只是为了好玩,我创建了一个web.loaders文件和一个web.loaders.config文件。与

  ConfigurationManager.OpenExeConfiguration(@\\server\folders\web.local); 

它完全加载\server\folders\web.local.config,但抛出异常没有web.local文件。



所以有很多方法来获得这个小东西加载,但不是有一个比使用temp .web文件更好或某事?

解决方案

ConfigurationManager.OpenExeConfiguration 对于可执行文件,所以真的期望一个.exe文件的完整路径。配置文件应该在与.exe相同的目录中,并且扩展名为.exe.config,因此它不适合加载ASP.NET网站的配置。一个更好的选择是使用 WebConfigurationManager OpenWebConfiguration 方法是可以的,如果你打开配置从网站内,因为它期望的虚拟路径配置,但如果你打开它从像控制台应用程序,那么您需要查看 OpenMappedWebConfiguration 方法。 文档说明了如何实现此目的。



在Stack Overflow上还有一个以前的问题和答案显示如何做到这一点。


I'm trying to load an web.config from a server. Its placed at: \server\folders\web.config

when i try this:

 ConfigurationManager.OpenExeConfiguration(@"\\server\folders\web.config");

it searches for: web.config.config and fails.

 ConfigurationManager.OpenExeConfiguration(@"\\server\folders\web");

it fails, because there is no folder \server\folders\web\

So i tried several things and it seems as its checking wether the file the path is pointing on exists, and afterwards it appliers a .config and gets the config file.

Just for fun i created an web.loaders file, and a web.loaders.config file. with

 ConfigurationManager.OpenExeConfiguration(@"\\server\folders\web.local");

it loads the \server\folders\web.local.config perfectly, but throws Exceptions without the web.local file.

So there a many ways to get this small thing loaded, but isn't there a more nice one than using a temp .web file or something?

解决方案

ConfigurationManager.OpenExeConfiguration is intended for loading the configuration for an executable and so really expects the full path to an .exe file. The config file is expected to be in the same directory as the .exe and have the extension .exe.config, so it isn't really suitable for loading the configuration of an ASP.NET website. A better choice is to use the WebConfigurationManager. The OpenWebConfiguration method is okay if you're opening the config from within the site since it expects the virtual path to the config, but if you're opening it from something like a console app then you'll need to look at the OpenMappedWebConfiguration method. The documentation gives an example how you might achieve this.

There's also a previous question and answer on Stack Overflow showing how to do this.

这篇关于OpenExeConfiguration路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆