ConfigurationManager 在不同系统上查找不同文件

发布于 2024-09-14 17:57:21 字数 560 浏览 1 评论 0原文

我以最简单的方式使用配置管理器:

读:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2024-09-21 17:57:21

感谢您的回复,您的链接非常有帮助。
由于这是一个 .NET 问题(如上面的链接所述),我从与建议不同的角度解决了它:
由于我的配置文件很大并且需要读取和写入操作,因此我使用一个特殊的类来处理它 - configurationFileHelper

我所做的就是向此类添加一个静态构造函数,在其中查询文件的预期名称,并在必要时重命名现有文件以匹配它:

    static configurationFileHelper()
    {
        try
        {
            string fullFilename = Application.ProductName + ".exe.config";
            string expectedFilename = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
            if (!File.Exists(expectedFilename) && (File.Exists(fullFilename))
                    File.Move(fullFilename, expectedFilename);
        }
        catch { ; }
    }

希望这对某人有帮助......

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:

    static configurationFileHelper()
    {
        try
        {
            string fullFilename = Application.ProductName + ".exe.config";
            string expectedFilename = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
            if (!File.Exists(expectedFilename) && (File.Exists(fullFilename))
                    File.Move(fullFilename, expectedFilename);
        }
        catch { ; }
    }

Hope this is helpful to someone...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文