簡單介紹App.config
App.config檔案一般是存放資料庫連接配接字元串的。
下面來簡單介紹一下App.config檔案的修改和更新。
1, 向項目添加app.config檔案
右擊項目名稱,選擇“添加”→“添加建立項”,在出現的“添加新項”對話框中,選擇“添加應用程式配置檔案”;如果項目以前沒有配置檔案,則預設的檔案名稱為“app.config”,單擊“确定”。出現在設計器視圖中的app.config檔案為:
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 </configuration>
注意:在項目進行編譯後,在bin\Debuge檔案下,将出現兩個配置檔案,一個名為“Test.exe.config”,另一個名為“Test.vshost.exe.config”。第一個檔案為項目實際使用的配置檔案,在程式運作中所做的更改都将被儲存于此;第二個檔案為原代碼“app.config”的同步檔案,在程式運作中不會發生更改。
2, connectionStrings配置節:
下面添加第一個connectionStrings, code 如下:
1 <connectionStrings>
2 <clear/>
3 <add name="connect" connectionString="Data Source=localhost;Initial Catalog=test;User ID=sa;password=********"/>
4 </connectionStrings>
3, appSettings配置節:
appSettings配置節為整個程式的配置,如果是對目前使用者的配置,請使用userSettings配置節,其格式與以下配置書寫要求一樣。
1 <appSettings>
2 <add key="DownloadPath" value="" />
3 <add key="ClientSettingsProvider.ServiceUri" value="" />
4 </appSettings>
4, 讀取和更新app.config
對于app.config檔案的讀寫,參照了網絡文章:http://www.codeproject.com/csharp/ SystemConfiguration.asp标題為“Read/Write App.Config File with .NET 2.0”一文。
請注意:要使用以下的代碼通路app.config檔案,除添加引用System.Configuration外,還必須在項目添加對System.Configuration.dll的引用。
4.1 讀取connectionStrings配置節
1 ///<summary>
2 ///依據連接配接串名字connectionName傳回資料連接配接字元串
3 ///</summary>
4 ///<param name="connectionName">連接配接串名字</param>
5 ///<returns></returns>
6 private static string GetConnectionStringsConfig(string connectionName)
7 {
8 string connectionString =
9 ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();
10 return connectionString;
11 }
4.2 更新connectionStrings配置節
1 ///<summary>
2 ///更新連接配接字元串
3 ///</summary>
4 ///<param name="newName">連接配接字元串名稱</param>
5 ///<param name="newConString">連接配接字元串内容</param>
6 ///<param name="newProviderName">資料提供程式名稱</param>
7 private static void UpdateConnectionStringsConfig(string newName,
8 string newConString,
9 string newProviderName)
10 {
11 bool isModified = false; //記錄該連接配接串是否已經存在
12 //如果要更改的連接配接串已經存在
13 if (ConfigurationManager.ConnectionStrings[newName] != null)
14 {
15 isModified = true;
16 }
17 //建立一個連接配接字元串執行個體
18 ConnectionStringSettings mySettings =
19 new ConnectionStringSettings(newName, newConString, newProviderName);
20 // 打開可執行的配置檔案*.exe.config
21 Configuration config =
22 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
23 // 如果連接配接串已存在,首先删除它
24 if (isModified)
25 {
26 config.ConnectionStrings.ConnectionStrings.Remove(newName);
27 }
28 // 将新的連接配接串添加到配置檔案中.
29 config.ConnectionStrings.ConnectionStrings.Add(mySettings);
30 // 儲存對配置檔案所作的更改
31 config.Save(ConfigurationSaveMode.Modified);
32 // 強制重新載入配置檔案的ConnectionStrings配置節
33 ConfigurationManager.RefreshSection("ConnectionStrings");
34 }
4.3 讀取appStrings配置節
///<summary>
///傳回*.exe.config檔案中appSettings配置節的value項
///</summary>
///<param name="strKey"></param>
///<returns></returns>
private static string GetAppConfig(string strKey)
{
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == strKey)
{
return ConfigurationManager.AppSettings[strKey];
}
}
return null;
}
4.4 更新connectionStrings配置節
///<summary>
///在*.exe.config檔案中appSettings配置節增加一對鍵、值對
///</summary>
///<param name="newKey"></param>
///<param name="newValue"></param>
private static void UpdateAppConfig(string newKey, string newValue)
{
bool isModified = false;
foreach (string key in ConfigurationManager.AppSettings)
{
if(key==newKey)
{
isModified = true;
}
}
// Open App.Config of executable
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// You need to remove the old settings object before you can replace it
if (isModified)
{
config.AppSettings.Settings.Remove(newKey);
}
// Add an Application Setting.
config.AppSettings.Settings.Add(newKey,newValue);
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}
以上是WPF中App.config檔案的更新,在這總結一下。
注意, .Net隻在程式剛啟動的時候,讀取一次配置檔案,是以我們在修改app.config檔案之後,加上一行ConfigurationManager.RefreshSection("appSettings")來重新整理記憶體中的"appSettings",這樣進而實作實時更改。