是否可以将整个App.Config文件重新定位到自定义路径?
似乎有点奇怪的是,配置文件和exe驻留在同一个文件夹中,Windows的新approcah将所有程序设置保存在c:\ProgramData和all中。
我们还有一个额外的要求,那就是以编程方式指定在哪里可以找到app.config文件。这样做的原因是我们从相同的exe产生不同的服务实例,并希望将每个服务的app.config存储在该服务的设置文件夹中的c:\ProgramData\下。
发布于 2009-12-03 18:12:32
每个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);
希望这能有所帮助。
发布于 2012-10-03 20:23:27
如果仍然相关,我们使用了以下我在Stack Overflow上找到的另一个问题的建议答案……
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")
当我们在从DLL加载app.config时遇到问题时,我们工作得很好…
发布于 2014-04-15 18:07:25
这对我很有效..(摘自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;
或任何获取应用程序配置的操作,则使用新路径。
希望这能帮助其他有同样问题的人!
https://stackoverflow.com/questions/1838619
复制相似问题