C# 讀取與修改設定檔

來源:互聯網
上載者:User

標籤:

System.Configuration.ConfigurationSettings.AppSettings["Key"]; 
但是現在FrameWork2.0已經明確表示此屬性已經過時。並建議改為ConfigurationManager或WebConfigurationManager。並且AppSettings屬性是唯讀,並不支援修改屬性值.

但是要想調用ConfigurationManager必須要先在工程裡添加system.configuration.dll程式集的引用。(在解決方案管理器中右鍵點擊工程名稱,在右鍵菜單中選擇添加引用,.net TablePage下即可找到)添加引用後可以用 String str = ConfigurationManager.AppSettings["Key"]來擷取對應的值了。

更新設定檔: 
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
//添加

cfa.AppSettings.Settings.Add("key", "Name")

//修改

cfa.AppSettings.Settings["BrowseDir"].Value = "name";

最後調用 
cfa.Save(); 
當前的設定檔更新成功。

ConfigurationManager.RefreshSection("appSettings");// 重新整理命名節,在下次檢索它時將從磁碟重新讀取它。記住應用程式要重新整理節點

C# 讀取與修改設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.