ConfigurationManager.RefreshSection(“AppSettings”) 不起作用
我试图让(ClickOnce 部署的)应用程序读取非默认配置文件,具体取决于设置的环境变量
_envName = System.Environment.GetEnvironmentVariable("ENV");
if (_envName == null)
throw new Exception ("The ENV environemnt variable must be set");
string envFileName = "app." + _envName.ToLower() + ".config";
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = envFileName;
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("AppSettings");
这不起作用!即,如果环境变量 ENV 设置为 dvlp,则代码会运行,但无法访问文件 app.dvlp.config 中的值。
谁能明白为什么这不起作用?或者建议我可以为每个环境/机器创建单独的文件 app.xxx.config 并相应地使用它们。
这确实应该简单得多。
谢谢瑞安
I am trying to get a (ClickOnce deployed) application to read the non-default config file, depending on an environment variable set on the
_envName = System.Environment.GetEnvironmentVariable("ENV");
if (_envName == null)
throw new Exception ("The ENV environemnt variable must be set");
string envFileName = "app." + _envName.ToLower() + ".config";
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = envFileName;
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("AppSettings");
This doesn't work! i.e. if the env varibale ENV is set to dvlp, the code runs, but values in the file app.dvlp.config are not accessible.
Can anyone see why this doesn't work? Or suggest a way I can have separate files, app.xxx.config for each environment / machine and have them used accordingly.
This really ought to be much more simple.
Thanks
Ryan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里是来自 Microsoft ClickOnce 产品负责人的一篇博客文章,介绍了处理不同构建配置的 app.config 文件的方法。希望有帮助。
Here is a blog article from the ClickOnce product lead at Microsoft with a way to handle app.config files for different build configurations. Hope it helps.
我很确定您无法通过 ClickOnce 安装访问沙盒区域之外的内容。
http://msdn.microsoft.com/en-us/library/d8saf4wy。 aspx
也就是说,如果我错了,我也很想知道答案:)
I'm pretty sure you can't access stuff outside of your sand boxed area with a ClickOnce install.
http://msdn.microsoft.com/en-us/library/d8saf4wy.aspx
That said if I'm wrong I'd love to know the answer too :)
您传递的参数是否正确?我注意到您有大写 AppSettings 而不是 appSettings
are you passing correct parameter I noticed you have capital AppSettings not appSettings