2

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

读:

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 后,它工作正常。

我的问题是:啊?!!?有任何想法吗?

4

1 回答 1

0

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

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

于 2010-08-23T07:08:02.053 回答