是否可以将整个App.Config文件重定位到自定义路径?
[配置文件与exe驻留在同一文件夹中似乎有点奇怪,Windows的新做法是将所有程序设置保存在c:\ ProgramData中,以及全部。
我们还有一个附加要求,就是以编程方式指定在哪里可以找到app.config文件。原因是我们从相同的exe生成了不同的服务实例,并希望将每个服务的app.config存储在该服务的settings文件夹中的c:\ ProgramData \\下。
每个AppDomain都有/可以有自己的配置文件。 CLR主机创建的默认AppDomain使用programname.exe.config。如果要提供自己的配置文件,请创建单独的AppDomain。示例:
// get the name of the assembly
string exeAssembly = Assembly.GetEntryAssembly().FullName;
// setup - there you put the path to the config file
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = System.Environment.CurrentDirectory;
setup.ConfigurationFile = "<path to your config file>";
// create the app domain
AppDomain appDomain = AppDomain.CreateDomain("My AppDomain", null, setup);
// create proxy used to call the startup method
YourStartupClass proxy = (YourStartupClass)appDomain.CreateInstanceAndUnwrap(
exeAssembly, typeof(YourStartupClass).FullName);
// call the startup method - something like alternative main()
proxy.StartupMethod();
// in the end, unload the domain
AppDomain.Unload(appDomain);
希望有所帮助。
[如果仍然相关,我们将以下内容用于我对堆栈溢出的另一个问题的另一个建议答案中。
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")
当我们遇到仅从DLL加载app.config的问题时,对我们非常有用...
这对我有用..(摘自http://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx)
// open config
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// update appconfig file path
config.AppSettings.File = "C:\\dev\\App.config";
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload in memory of the changed section.
ConfigurationManager.RefreshSection("appSettings");
然后打电话时
NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;
或任何用于获取应用程序配置的操作,将使用新路径。
希望这可以帮助遇到相同问题的其他人!
我知道这是一个古老的问题,但是对于那些[[just想要将其app.config放在其他位置然后位于其二进制输出生成位置的人,以下功能可以按照Microsoft的预期进行(因此无需重新安装) -读取并将文件重新写入磁盘)
示例app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<appSettings file="..\Config\settings.config">
<add key="port" value="1001"/>
</appSettings>
</configuration>
注意file =“ .. \ Config \ settings.config”。您完全可以自由定义要用户更改设置的位置的路径。
实际配置文件示例
<?xml version="1.0" encoding="utf-8"?> <appSettings> <add key="port" value="1234"/> </appSettings>
在运行时,设置port
的值为1234。更多信息,请参见msdn
static public class ConfigHack {
static public void OverrideAppConfig(string path) {
((AppDomainSetup)
typeof(AppDomain)
.GetField("_FusionStore", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(AppDomain.CurrentDomain))
.ConfigurationFile = path;
}
static public void ResetConfigManager() {
typeof(ConfigurationManager)
.GetField("s_initState", BindingFlags.Static | BindingFlags.NonPublic)
.SetValue(null, 0);
}
}
我只在.NET2上使用过它,但是在反射器中它在4中看起来相同。当然,我不建议运送此:P我只将其用于快速内部操作。
ConfigurationManager.OpenExeConfiguration Method (String)
根据更改,可以使用
OpenExeConfiguration
。如果您确实要重定位配置文件,则必须创建自己的应用程序域。在设置应用程序域的过程中,您可以指定配置文件的位置。BTW,.NET配置文件不适用于配置,至少不是用户可以修改的类型:它们不像INI文件或注册表。如果您想灵活了解配置的来源,最好将其单独存储。
元素简化了组件的维修组件。如果一个或多个应用程序使用具有驻留在众所周知的位置,配置使用以下应用程序的文件组装可以使用元素包括装配配置文件,而不是包含直接配置信息。当组件装配是服务,更新通用配置文件提供更新所有人的配置信息使用该程序集的应用程序