2012-11-08 38 views
4

我正在使用.NET 4.0,我想使用app.config文件來存儲相同的參數設置。我做了以下。我使用項目屬性中的Settings選項卡來創建我的參數。C#applicationSettings:如何更新app.config?

這增加了信息的app.config文件是這樣的:

<MyApp.Properties.Settings> 
     <setting name="param1" serializeAs="String"> 
     <value>True</value> 
     </setting> 
<MyApp.Properties.Settings> 

在我的視圖模型(在我的代碼),我可以訪問的信息是這樣的:

bool myBool = MyApp.Properties.Default.param1; 

當我嘗試更改配置文件中的值,我試試這個:

Properties.Settings.Default.param1 = false; 

但是這個cau出現錯誤,那param1是隻讀的。

那麼如何從我的代碼更新我的配置文件?

+1

可能是試試這個:http://stackoverflow.com/questions/1357240/change-the-value-in-app-config-file-dynamicallay –

+0

在這個解決方案,他們使用config.AppSettings。 AppSettings不被棄用?我想使用ApplicationSettings,因爲這使用了輸入參數。 –

+0

編輯我的答案與EDIT2給出一個鏈接到一個鏈接到一個代碼,這可能是你的解決方案:) –

回答

3

嗯,我讀了Hari Gillala的鏈接,其中一位用戶建議直接編輯app.config文件,這是一個xml文件。

所以在項目屬性 - >設置我創建我需要的參數。然後,加載代碼的參數我做了以下內容:

_myViewModelProperty = MyApp.Properties.Settings.Default.MyParam1; 

這樣一來,我可以很容易地讀取配置參數的信息。是打字,所以在設計時間,我可以看到是否正確或不正確。

要更新de config文件,我使用.NET的xml庫編輯app.config文件。

System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); 
    xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 
    System.Xml.XmlNode node; 
    node = xml.SelectSingleNode("configuration/applicationSettings/MyApp.Properties.Settings/setting[@name='myparam1']"); 
    node.ChildNodes[0].InnerText = myNewValue; 
    xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

這樣,我創建了一個xml文檔(xml變量)並加載app.config文件的信息。然後,我搜索要更新的節點,更新其信息(InnerText屬性),最後保存更改。

通過這種方式,我可以更新app.config。這是我想要的,因爲在便攜式應用程序中,只有一個用戶會使用它,並且我希望配置應用於運行應用程序的任何計算機中。

2

您應該使用Properties.Settings執行此操作。 有關更多信息,您可以查看此documentation。在評論和進一步的搜索協議之後

//modify 
Properties.Settings.Default.param1 = false; 
//save the setting 
Properties.Settings.Default.Save(); 

編輯:

我的建議,以達到預期的結果將是切換到的AppSettings。 這是因爲,經過一些搜索後,我發現appsettings更改了Application Data文件夾中的.config文件(在我的機器上運行一些測試證實了這一點)。

請看this question的回答評論。

我不知道是否有周圍的一些工作,但如果你想你的應用程序有一個便攜式app.config文件,我認爲唯一的辦法就是切換到的AppSettings,我相信可以保存更改在程序文件夾中找到的app.config中。

編輯2:可能的解決方法

我發現了一個可能的解決方案,使您的應用程序移植! 您可以更改「設置」使用的提供程序以保存創建自定義提供程序的應用程序設置。

this question的回答提供了一個指向應用程序設置便攜式的代碼的鏈接。我想你試試吧

+1

嗯,真的,我用這種方式,Properties.Settings.Default,但我寫錯了我的代碼。這樣就是我如何得到錯誤。 –

+0

我認爲您的設置範圍是應用程序,您必須將其更改爲用戶以便能夠在運行時對其進行修改 –

+0

在app.config中,您可以將其設置從移動... < applicationSettings>部分添加到部分,或者使用GUI,在設置配置 –

9

這裏是我的函數來更新或添加一個條目到app.config中的「applicationSettings」部分。可能有更好的辦法,但這對我很有用。如果任何人都可以提出更好的方法,請分享,我們會一直在尋找更好的東西。

static string APPNODE = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Settings"; 
    static DateTime now = DateTime.Now; 
    Utilities.UpdateConfig(APPNODE, "lastQueryTime", now.ToString()); 

    static public void UpdateConfig(string section, string key, string value) 
    { 
     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

     ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["applicationSettings"].Sections[section]; 
     SettingElement element = applicationSettingsSection.Settings.Get(key); 

     if (null != element) 
     { 
      applicationSettingsSection.Settings.Remove(element); 
      element.Value.ValueXml.InnerXml = value; 
      applicationSettingsSection.Settings.Add(element); 
     } 
     else 
     { 
      element = new SettingElement(key, SettingsSerializeAs.String); 
      element.Value = new SettingValueElement(); 
      System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
      element.Value.ValueXml = doc.CreateElement("value"); 

      element.Value.ValueXml.InnerXml = value; 
      applicationSettingsSection.Settings.Add(element); 
     } 

     config.Save(ConfigurationSaveMode.Modified); 
     ConfigurationManager.RefreshSection("applicationSettings");    
    } 
相關問題