.Net設定檔讀取及修改方法封裝(未加密)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

.Net平台下開發時,需要將有些資料存放區在XML文檔中,而我們的首選即是預設建立的app.Config了。建立流程如下:

第一步:在啟動工程上右鍵->添加->建立項。如:

第二步:建立設定檔,修改名稱,切記要將預設的App1.config修改為App.config,因為預設尋找的設定檔名稱是App.config。如果不改名稱將找不到,如:

第三步:添加相應的connectionStrings和appSettings,如:

那麼至此,我們的設定檔已經建立好了。那麼建立好的設定檔是方便我們使用的,不管是我們讀取設定檔中的值還是我們要修改設定檔中的有些配置項。當系統交付客戶時,不能每次直接讓客戶操作配置,更何況一般設定檔都是要加密的。所以我在其他部落格園找了一些封裝好的方法可供參考:

代碼如下:

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Configuration;  6   7 namespace WPFControlsDemo.Control  8 {  9     /// <summary> 10     /// 設定檔協助類 11     /// </summary> 12     public class ConfigHelper 13     { 14         #region ConnectionStrings 15  16         /// <summary> 17         /// 根據串連串名字connectionName返回資料連線字串 18         /// </summary> 19         /// <param name="connectionName">串連串名字</param> 20         /// <returns>資料連線字串</returns> 21         private static string GetConnectionStringsConfig(string connectionName) 22         { 23             string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ToString(); 24             return connectionString; 25         } 26  27         /// <summary> 28         /// 更新連接字串 29         /// </summary> 30         /// <param name="newName">連接字串名稱</param> 31         /// <param name="newConString">連接字串內容</param> 32         /// <param name="newProviderName">資料提供者名稱</param> 33         private static void UpdateConnectionStringConfig(string newName, string newConString, string newProviderName) 34         { 35             //記錄該連接字串是否已經存在 36             bool isModified = false; 37             //如果要更改的連接字串已經存在 38             if (ConfigurationManager.ConnectionStrings[newName] != null) 39             { 40                 isModified = true; 41             } 42  43             //建立一個連接字串執行個體 44             ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString, newProviderName); 45             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 46  47             //如果串連串已存在,首先刪除它 48             if (isModified) 49             { 50                 config.ConnectionStrings.ConnectionStrings.Remove(newName); 51             } 52             //將新的串連串添加到設定檔 53             config.ConnectionStrings.ConnectionStrings.Add(mySettings); 54             //儲存對設定檔所做的更改 55             config.Save(ConfigurationSaveMode.Modified); 56             //強制重新載入設定檔的ConnectionStrings配置節 57             ConfigurationManager.RefreshSection("ConnectionStrings"); 58         } 59  60         #endregion 61  62         #region AppStrings 63  64         /// <summary> 65         /// 返回config檔案中的appSettings配置節的value項 66         /// </summary> 67         /// <param name="strKey">appSettings配置節關鍵字</param> 68         /// <returns>APPSettings配置節值</returns> 69         private static string GetAppConfig(string strKey) 70         { 71             foreach (string key in ConfigurationManager.AppSettings) 72             { 73                 if (key == strKey) 74                 { 75                     return ConfigurationManager.AppSettings[strKey]; 76                 } 77             } 78  79             return null; 80         } 81  82         /// <summary> 83         /// 在config檔案中appSettings配置節增加一對鍵,值對 84         /// </summary> 85         /// <param name="newKey"></param> 86         /// <param name="newValue"></param> 87         private static void UpdateAppConfig(string newKey, string newValue) 88         { 89             bool isModified = false; 90             foreach (string key in ConfigurationManager.AppSettings) 91             { 92                 if (key == newKey) 93                 { 94                     isModified = true; 95                 } 96             } 97             //開啟設定檔 98             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 99             //刪除已存在的節點100             if (isModified)101             {102                 config.AppSettings.Settings.Remove(newKey);103             }104             //增加appSettings節點105             config.AppSettings.Settings.Add(newKey, newValue);106             //儲存設定檔107             config.Save(ConfigurationSaveMode.Modified);108             //強制重新整理109             ConfigurationManager.RefreshSection("appSettings");110         }111 112         #endregion113     }114 }

.Net設定檔讀取及修改方法封裝(未加密)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.