2010-08-15 52 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 { ; } 
    } 

希望這對某人有幫助......