C# 在不同于二进制文件的位置访问 App.config
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/75978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing App.config in a location different from the binary
提问by Chris Comeaux
In a .NET Win console application, I would like to access an App.config file in a location different from the console application binary. For example, how can C:\bin\Text.exe get its settings from C:\Test.exe.config?
在 .NET Win 控制台应用程序中,我想访问与控制台应用程序二进制文件不同的位置中的 App.config 文件。例如,C:\bin\Text.exe 如何从 C:\Test.exe.config 获取其设置?
采纳答案by jeff.willis
using System.Configuration;
Configuration config =
ConfigurationManager.OpenExeConfiguration("C:\Test.exe");
You can then access the app settings, connection strings, etc from the config instance. This assumes of course that the config file is properly formatted and your app has read access to the directory. Notice the path is not"C:\Test.exe.config" The method looks for a config file associated with the file you specify. If you specify "C:\Test.exe.config" it will look for "C:\Test.exe.config.config" Kinda lame, but understandable, I guess.
然后,您可以从配置实例访问应用程序设置、连接字符串等。这当然假设配置文件格式正确并且您的应用程序具有对该目录的读取访问权限。注意路径不是“C:\Test.exe.config” 该方法查找与您指定的文件关联的配置文件。如果您指定“C:\Test.exe.config”,它将查找“C:\Test.exe.config.config” 有点蹩脚,但可以理解,我猜。
Reference here: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openexeconfiguration.aspx
参考这里:http: //msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openexeconfiguration.aspx
回答by Santiago Palladino
Use the following (remember to include System.Configuration assembly)
使用以下(记得包括 System.Configuration 程序集)
ConfigurationManager.OpenExeConfiguration(exePath)
回答by Michael Meadows
You can set it by creating a new app domain:
您可以通过创建新的应用程序域来设置它:
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ConfigurationFile = fileLocation;
AppDomain add = AppDomain.CreateDomain("myNewAppDomain", securityInfo, domainSetup);
回答by CodeNaked
It appears that you can use the AppDomain.SetData
method to achieve this. The documentation states:
看来您可以使用该AppDomain.SetData
方法来实现这一点。该文件指出:
You cannot insert or modify system entries with this method.
您不能使用此方法插入或修改系统条目。
Regardless, doing so does appear to work. The documentation for the AppDomain.GetData
method lists the system entries available, of interest is the "APP_CONFIG_FILE"
entry.
无论如何,这样做似乎确实有效。该AppDomain.GetData
方法的文档列出了可用的系统条目,感兴趣的是"APP_CONFIG_FILE"
条目。
If we set the "APP_CONFIG_FILE"
before any application settings are used, we can modify where the app.config
is loaded from. For example:
如果我们"APP_CONFIG_FILE"
在使用任何应用程序设置之前设置 ,我们可以修改app.config
从何处加载 。例如:
public class Program
{
public static void Main()
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Temp\test.config");
//...
}
}
I found this solution documented in this blogand a more complete answer (to a related question) can be found here.