2010-08-15 57 views
2

我使用配置管理器中最简单的方法: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”(相同,无/ e“.exe”)

另一个有趣的细节是,在有问题的机器上安装VS之后 - 它工作正常。

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

+0

你可以连接行为< - Windows版本吗? – 2010-08-15 09:34:49

+1

猜测。但是,如果它在安装VS之后能够工作,它可以在.NET3.5 SP1中得到解决。 编辑 - 发现有关此问题的MS Connect页面:https://connect.microsoft.com/VisualStudio/feedback/details/290821/configurationmanager-openexeconfiguration-misbehaves-on-some-platforms – 2010-08-15 09:37:12

+0

谢谢...感觉很好当你有人责怪... – Nissim 2010-08-15 13:24:03

回答

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 { ; } 
    } 

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