首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将app.config文件重新定位到自定义路径

将app.config文件重新定位到自定义路径
EN

Stack Overflow用户
提问于 2009-12-03 16:59:01
回答 7查看 60.2K关注 0票数 31

是否可以将整个App.Config文件重新定位到自定义路径?

似乎有点奇怪的是,配置文件和exe驻留在同一个文件夹中,Windows的新approcah将所有程序设置保存在c:\ProgramData和all中。

我们还有一个额外的要求,那就是以编程方式指定在哪里可以找到app.config文件。这样做的原因是我们从相同的exe产生不同的服务实例,并希望将每个服务的app.config存储在该服务的设置文件夹中的c:\ProgramData\下。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2009-12-03 18:12:32

每个AppDomain都有/可以有自己的配置文件。CLR主机创建的默认AppDomain使用programname.exe.config;如果您希望提供自己的配置文件,请创建单独的AppDomain。示例:

代码语言:javascript
运行
复制
// 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);

希望这能有所帮助。

票数 18
EN

Stack Overflow用户

发布于 2012-10-03 20:23:27

如果仍然相关,我们使用了以下我在Stack Overflow上找到的另一个问题的建议答案……

代码语言:javascript
运行
复制
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")

当我们在从DLL加载app.config时遇到问题时,我们工作得很好…

票数 40
EN

Stack Overflow用户

发布于 2014-04-15 18:07:25

这对我很有效..(摘自http://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx)

代码语言:javascript
运行
复制
// 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");

然后当你调用

代码语言:javascript
运行
复制
NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;

或任何获取应用程序配置的操作,则使用新路径。

希望这能帮助其他有同样问题的人!

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1838619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档