1. 向項目添加app.config檔案:
右擊項目名稱,選擇“添加”→“添加建立項”,在出現的“添加新項”對話方塊中,選擇“添加應用程式設定檔”;如果項目以前沒有設定檔,則預設的檔案名稱為“app.config”,單擊“確定”。出現在設計器視圖中的app.config檔案為:
view plaincopy to clipboardprint?
- <?xmlversion="1.0"encoding="utf-8" ?>
- <configuration>
- </configuration>
<?xmlversion="1.0"encoding="utf-8" ?><br /><configuration><br /></configuration><br />
在項目進行編譯後,在bin\Debuge檔案下,將出現兩個設定檔(以本項目為例),一個名為“JxcManagement.EXE.config”,另一個名為“JxcManagement.vshost.exe.config”。第一個檔案為項目實際使用的設定檔,在程式運行中所做的更改都將被儲存於此;第二個檔案為原代碼“app.config”的同步檔案,在程式運行中不會發生更改。
2. connectionStrings配置節:
請注意:如果您的SQL版本為2005 Express版,則預設安裝時SQL伺服器執行個體名為localhost\SQLExpress,須更改以下執行個體中“Data Source=localhost;”一句為“Data Source=localhost\SQLExpress;”,在等號的兩邊不要加上空格。
view plaincopy to clipboardprint?
- <!--資料庫連接串-->
- <connectionStrings>
- <clear />
- <addname="conJxcBook"
- connectionString="Data Source=localhost;Initial Catalog=jxcbook;User ID=sa;password=********"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
<!--資料庫連接串--><br /> <connectionStrings><br /> <clear /><br /> <addname="conJxcBook"<br /> connectionString="Data Source=localhost;Initial Catalog=jxcbook;User ID=sa;password=********"<br /> providerName="System.Data.SqlClient" /><br /> </connectionStrings><br />
3. appSettings配置節:
appSettings配置節為整個程式的配置,如果是對目前使用者的配置,請使用userSettings配置節,其格式與以下配置書寫要求一樣。
view plaincopy to clipboardprint?
- <!--進銷存管理系統初始化需要的參數-->
- <appSettings>
- <clear />
- <add key="userName" value="" />
- <add key="password" value="" />
- <add key="Department" value="" />
- <add key="returnValue" value="" />
- <add key="pwdPattern" value="" />
- <add key="userPattern" value="" />
- </appSettings>
<!--進銷存管理系統初始化需要的參數--><br /> <appSettings><br /> <clear /><br /> <addkey="userName"value="" /><br /> <addkey="password"value="" /><br /> <addkey="Department"value="" /><br /> <addkey="returnValue"value="" /><br /> <addkey="pwdPattern"value="" /><br /> <addkey="userPattern"value="" /><br /></appSettings><br />
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配置節
view plaincopy to clipboardprint?
- ///<summary>
- ///依據串連串名字connectionName返回資料連線字串
- ///</summary>
- ///<param name="connectionName"></param>
- ///<returns></returns>
- private static string GetConnectionStringsConfig(string connectionName)
- {
- string connectionString =
- ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();
- Console.WriteLine(connectionString);
- return connectionString;
- }
///<summary><br />///依據串連串名字connectionName返回資料連線字串<br />///</summary><br />///<param name="connectionName"></param><br />///<returns></returns><br />private static string GetConnectionStringsConfig(string connectionName)<br />{<br />string connectionString =<br /> ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();<br /> Console.WriteLine(connectionString);<br /> return connectionString;<br />}<br />
4.2 更新connectionStrings配置節
view plaincopy to clipboardprint?
- ///<summary>
- ///更新連接字串
- ///</summary>
- ///<param name="newName">連接字串名稱</param>
- ///<param name="newConString">連接字串內容</param>
- ///<param name="newProviderName">資料提供者名稱</param>
- private static void UpdateConnectionStringsConfig(string newName,
- string newConString,
- string newProviderName)
- {
- bool isModified = false; //記錄該串連串是否已經存在
- //如果要更改的串連串已經存在
- if (ConfigurationManager.ConnectionStrings[newName] != null)
- {
- isModified = true;
- }
- //建立一個連接字串執行個體
- ConnectionStringSettings mySettings =
- new ConnectionStringSettings(newName, newConString, newProviderName);
- // 開啟可執行檔設定檔*.exe.config
- Configuration config =
- ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- // 如果串連串已存在,首先刪除它
- if (isModified)
- {
- config.ConnectionStrings.ConnectionStrings.Remove(newName);
- }
- // 將新的串連串添加到設定檔中.
- config.ConnectionStrings.ConnectionStrings.Add(mySettings);
- // 儲存對設定檔所作的更改
- config.Save(ConfigurationSaveMode.Modified);
- // 強制重新載入設定檔的ConnectionStrings配置節
- ConfigurationManager.RefreshSection("ConnectionStrings");
- }
///<summary><br />///更新連接字串<br />///</summary><br />///<param name="newName">連接字串名稱</param><br />///<param name="newConString">連接字串內容</param><br />///<param name="newProviderName">資料提供者名稱</param><br />private static void UpdateConnectionStringsConfig(string newName,<br /> string newConString,<br /> string newProviderName)<br />{<br /> bool isModified = false; //記錄該串連串是否已經存在<br /> //如果要更改的串連串已經存在<br /> if (ConfigurationManager.ConnectionStrings[newName] != null)<br /> {<br /> isModified = true;<br /> }<br /> //建立一個連接字串執行個體<br /> ConnectionStringSettings mySettings =<br /> new ConnectionStringSettings(newName, newConString, newProviderName);<br /> // 開啟可執行檔設定檔*.exe.config<br /> Configuration config =<br /> ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);<br /> // 如果串連串已存在,首先刪除它<br /> if (isModified)<br /> {<br /> config.ConnectionStrings.ConnectionStrings.Remove(newName);<br /> }<br /> // 將新的串連串添加到設定檔中.<br /> config.ConnectionStrings.ConnectionStrings.Add(mySettings);<br /> // 儲存對設定檔所作的更改<br /> config.Save(ConfigurationSaveMode.Modified);<br /> // 強制重新載入設定檔的ConnectionStrings配置節<br /> ConfigurationManager.RefreshSection("ConnectionStrings");<br />}<br />
4.3 讀取appStrings配置節
view plaincopy to clipboardprint?
- ///<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;
- }
///<summary><br />///返回*.exe.config檔案中appSettings配置節的value項<br />///</summary><br />///<param name="strKey"></param><br />///<returns></returns><br />private static string GetAppConfig(string strKey)<br />{<br /> foreach (string key in ConfigurationManager.AppSettings)<br /> {<br /> if (key == strKey)<br /> {<br /> return ConfigurationManager.AppSettings[strKey];<br /> }<br /> }<br /> return null;<br />}<br />
4.4 更新connectionStrings配置節
view plaincopy to clipboardprint?
- ///<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");
- }
- ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
多層次configSections源地址
http://gb2013.blog.163.com/blog/static/21735301201021253713453/
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="student" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<student>
<add key="name" value="http://www.cnblogs.com/i80386/archive/2011/10/27/amily"/>
<add key="age" value="http://www.cnblogs.com/i80386/archive/2011/10/27/15"/>
<add key="sex" value="http://www.cnblogs.com/i80386/archive/2011/10/27/female"/>
</student>
</configuration>
注意事項:configSections 相當於定義變數,必須放在configuration
IDictionary istudent = (IDictionary)ConfigurationManager.GetSection("student");
string[] keys=new string[istudent.Keys.Count];
string[] values = new string[istudent.Keys.Count];
istudent.Keys.CopyTo(keys,0);
istudent.Values.CopyTo(values, 0);
foreach (var key in keys)
{
Console.WriteLine(key);
}
foreach (var value in values)
{
Console.WriteLine(value);
}
foreach (var key in istudent.Keys)
{
Console.WriteLine("{0}:{1}", key, istudent[key]);
}