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获取其设置?
4 个解决方案
#1
12
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
#2
8
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 blog and a more complete answer (to a related question) can be found here.
我发现此博客中记录了此解决方案,可以在此处找到更完整的答案(相关问题)。
#3
5
Use the following (remember to include System.Configuration assembly)
使用以下(记得包括System.Configuration程序集)
ConfigurationManager.OpenExeConfiguration(exePath)
#4
3
You can set it by creating a new app domain:
您可以通过创建新的应用域来设置它:
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ConfigurationFile = fileLocation;
AppDomain add = AppDomain.CreateDomain("myNewAppDomain", securityInfo, domainSetup);
#1
12
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
#2
8
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 blog and a more complete answer (to a related question) can be found here.
我发现此博客中记录了此解决方案,可以在此处找到更完整的答案(相关问题)。
#3
5
Use the following (remember to include System.Configuration assembly)
使用以下(记得包括System.Configuration程序集)
ConfigurationManager.OpenExeConfiguration(exePath)
#4
3
You can set it by creating a new app domain:
您可以通过创建新的应用域来设置它:
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ConfigurationFile = fileLocation;
AppDomain add = AppDomain.CreateDomain("myNewAppDomain", securityInfo, domainSetup);