ConfigurationManager 在不同系统上查找不同文件
我以最简单的方式使用配置管理器:
读:
ConfigurationManager.AppSettings["Foo"]
写:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["Foo"].Value = value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
问题是在不同的计算机上安装应用程序后 - 有些正在寻找文件:“My.Application.exe.config” 而其他人则寻找“My.Application.config”(相同,没有“.exe”)
另一个有趣的细节是,在有问题的机器上安装 VS 后,它可以正常工作。
我的问题是:啊?!!? 有什么想法吗?
I'm using configuration manager in the simplest way:
Read:
ConfigurationManager.AppSettings["Foo"]
Write:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["Foo"].Value = value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
The problem is that after installing the application on different machines - some are looking for the file: "My.Application.exe.config"
while others look for "My.Application.config" (same, w/o the ".exe")
Another interesting detail is that after installing VS on the problematic machines - it works ok.
And my question is: Ah?!!?
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您的回复,您的链接非常有帮助。
由于这是一个 .NET 问题(如上面的链接所述),我从与建议不同的角度解决了它:
由于我的配置文件很大并且需要读取和写入操作,因此我使用一个特殊的类来处理它 -
configurationFileHelper
。我所做的就是向此类添加一个静态构造函数,在其中查询文件的预期名称,并在必要时重命名现有文件以匹配它:
希望这对某人有帮助......
Thanks for the responses, your links were very helpful.
Since this is a .NET issue (as described in the links above), I tackled it from a different angle than suggested:
Since my configuration file is vast and demands both read and write operations, i'm using a special class to handle it -
configurationFileHelper
.What I did was adding a static constructor to this class, in which i'm inquiring the expected name for the file, and, if necessary, renaming the existing file to match it:
Hope this is helpful to someone...