ConfigurationManager.OpenExeConfiguration - 加载错误的文件?

Posted

技术标签:

【中文标题】ConfigurationManager.OpenExeConfiguration - 加载错误的文件?【英文标题】:ConfigurationManager.OpenExeConfiguration - loads the wrong file? 【发布时间】:2010-11-08 05:00:36 【问题描述】:

我在一个项目中添加了多个 app.config(每个都有不同的名称)文件,并将它们设置为在每次构建时复制到输出目录。

我尝试使用以下方法访问每个文件的内容:

System.Configuration.Configuration o = ConfigurationManager.OpenExeConfiguration(@"app1.config");

代码运行,但 o.HasFile 以 False 结尾,而 o.FilePath 以“app1.config.config”结尾。如果我改成代码:

System.Configuration.Configuration o = ConfigurationManager.OpenExeConfiguration(@"app1");

然后代码炸弹“加载配置文件时出错:参数'exePath'无效。参数名称:exePath”。

如果我复制/粘贴配置文件(所以我最终得到 app1.config 和 app1.config.config),那么代码运行良好,但是,我认为这不是一个好的解决方案。因此,我的问题是:如何使用 ConfigurationManager.OpenExeConfiguration 正确加载配置文件?

【问题讨论】:

.net 提供了一篇好文章:docs.microsoft.com/en-us/dotnet/api/… 一篇更好的文章:codeproject.com/Articles/19675/… 【参考方案1】:

为了完全避免这个问题,您可以将配置文件作为 XML 文件读取,例如:

using System.Xml;
using System.Xml.XPath;    

XmlDocument doc = new XmlDocument();
doc.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\..\\..\\..\\MyWebProject\\web.config");
string value = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='MyKeyName']").Attributes["value"].Value;

【讨论】:

感谢 XPath 方式。我已经使用 ConfigurationManager 大约两天了,Configuration.AppSettings.Settings.Count = 0。您的方法至少为我设置了正确的值。【参考方案2】:
using System.Reflection;

try

    Uri UriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
    string appPath = UriAssemblyFolder.LocalPath;

    //Open the configuration file and retrieve 
    //the connectionStrings section.
    Configuration config = ConfigurationManager.
        OpenExeConfiguration(appPath + @"\" + exeConfigName);

    ConnectionStringsSection section =
        config.GetSection("connectionStrings")
        as ConnectionStringsSection;

至少,这是我在为我的控制台/GUI 应用程序加密和解密 connectionStrings 部分时使用的方法。 exeConfigName 是可执行文件的名称,包括 .exe。

【讨论】:

【参考方案3】:

我不记得在哪里找到了这个解决方案,但这是我设法加载特定 exe 配置文件的方法:

ExeConfigurationFileMap map = new ExeConfigurationFileMap  ExeConfigFilename = "EXECONFIG_PATH" ;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

【讨论】:

行得通!疯狂的是,弄清楚如何从指定的确切路径读取糟糕的配置文件需要做很多工作,但是谢谢。 如何使用生成的config.AppSettings["somekey"](或config.AppSettings.Settings[])而不出现访问权限错误? @Rubistro 您必须检查该项目是否存在(否则 config.AppSettings["somekey"] 返回 null),然后使用 .Value 布伦特,这里是如何使用生成的配置而不会出现访问权限错误: var settings = config.GetSection("SectionName") as AppSettingsSection; if (settings != null) Console.Write(settings.Settings["ConfigName"].Value);【参考方案4】:

根据http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3943ec30-8be5-4f12-9667-3b812f711fc9,参数是exe的位置,然后该方法会查找与该exe对应的config(我猜exePath的参数名称现在有意义!)。

【讨论】:

请注意,您可以将任何路径传递给程序集,而不仅仅是 exe。所以你给“SomeLib.dll”,它会打开“SomeLib.dll.config”。当您的 .NET 项目实际上只是另一个应用程序的插件时,它很有用,您不想在其可执行文件旁边为其部署 .config。 然而,当我将一个名为 app.config 的文件放在 dll 的目录中时,使用“app”作为 exePath 参数,我必须将文件名更改为 app 才能正常工作。 ..

以上是关于ConfigurationManager.OpenExeConfiguration - 加载错误的文件?的主要内容,如果未能解决你的问题,请参考以下文章