c# - ConfigurationManager 在不同的系统上寻找不同的文件

标签 c# winforms configuration-files 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 之后 - 它工作正常。

我的问题是:啊?!!? 有什么想法吗?

最佳答案

感谢您的回复,您的链接非常有帮助。 由于这是一个 .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 { ; }
    }

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

关于c# - ConfigurationManager 在不同的系统上寻找不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3486804/

相关文章:

c# - 网络浏览器字体大小(以像素为单位)

c# - 如何自动打印tiff文件?

c# - 如何根据 C# 中的一系列值计算颜色?

c# - 重命名 WinForms 窗体的标题

winforms - 提交 Subversion 后为 "Unable to update dependencies of the project"

c# - 从类型定义字符串实例化 System.Type

c# - Visual Studio 选项上的行 Winform 控件

c# - 如何使用 Winforms 或 WPF 构建表单完成/文档合并应用程序?

java - 使用属性配置文件运行 .jar 文件

php - 在 PHP 项目中管理本地/开发/生产配置文件的简单方法是什么?