DotNet中配置文件的使用(一)
在.Net平臺下的配置文件主要使用在Web開發和桌面開發中,對應的配置文件類型也不一樣,Web中為Web.config文件,但是在桌面應用中為App.config文件。
當然使用上也稍微有些區別,下文將會闡述。
?
配置文件結構大致如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration><!--自定義配置節點--><configSections><sectionGroup name="OwnSectionA"><section name="AAA" type="AppLibrary.Configuration.MySelfInfoHandle,AppLibrary,Version=1.0.0.0"/><section name="BBB" type="System.Configuration.SingleTagSectionHandler"/></sectionGroup><sectionGroup name="OwnSectionB"><section name="aaa" type="System.Configuration.SingleTagSectionHandler"/><section name="bbb" type="System.Configuration.SingleTagSectionHandler"/></sectionGroup></configSections><!--自定義節點內容區域--><OwnSectionA><AAA><add key="name" value="huchen's homepage"/><add key="version" value="1.0"/></AAA></OwnSectionA><!--讀取修改配置節點--><appSettings><clear/><add key ="Access" value="/Date/mvp.accdb"/><add key="Sql" value="null"/></appSettings></configuration>對于配置文件的使用主要分兩種類型:
使用之前請確保項目已經引用了System.configuration程序集。
(一):系統預定義的配置節點(大家所熟悉的appSettings節點以及Web.config中connectionStrings節點)
對于connectionStrings配置節點的使用簡單介紹,因為大家太熟悉了,主要使用方法看如下代碼:
<connectionStrings><add name="SQLConn" connectionString="data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=nORTHWIND"providerName="System.Data.SqlClient" /><add name="SQLConnB" connectionString="server=.\SQLEXPRESS;database=Northwind;uid=sa;pwd=de"providerName="System.Data.SqlClient"/></connectionStrings>System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ToString()?????????????? 或者System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SQLConn"].ToString() 即可獲取配置文件中的值
對于appSettings配置節點的使用讀取與connectionStrings節點類似,只不過使用WebConfigurationManager.AppSettings和ConfigurationManager.AppSettings。
下面主要說一下appSettings節點的增加,修改和刪除,當然進行修改操作要確保用戶具有修改權限。直接看代碼:
Web中使用:
?????????? System.Configuration.Configuration?configuration?=?System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");?????????? System.Configuration.AppSettingsSection?app?=?configuration.AppSettings;?
?????????? app.Settings.Add(key,?value);?//增加
?????????? app.Settings[key].Value=value;?//修改
?????????? app.Settings.Remove(key);? //刪除
?????????? configuration.Save(System.Configuration.ConfigurationSaveMode.Modified);
WinForm中使用:
?????????? Configuration?config?=?ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);??????????? AppSettingsSection?app?=?config.AppSettings;?
?????????? app.Settings.Add(key,?value);?//增加
?????????? app.Settings[key].Value=value;?//修改
?????????? app.Settings.Remove(key);? //刪除
?????????? config.Save(ConfigurationSaveMode.Modified);
實時讀取小技巧:
ConfigurationManager.AppSettings 返回的是System.Collections.Specialized.NameValueCollection 表示可以通過索引訪問的關聯 String 鍵和 String 值的集合。
關于Winform下實時讀取技巧代碼段(通過在讀取并展現出來就不需要重新加載了,剛修改過的節點會立刻呈現出來):
??????????? Configuration?config?=?ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);??????????? List<string>?dir?=?new?System.Collections.Generic.List<string>();
??????????? AppSettingsSection?app?=?config.AppSettings;
???????????
??????????? string[]?str?=?app.Settings.AllKeys;
??????????? for?(int?i?=?0;?i?<?str.Length;?i++)
??????????? {
??????????????? dir.Add(str[i]?+ "||" +?app.Settings[str[i]].Value);
??????????? }
??????????? this.listBox1.DataSource?=dir;
(二):用戶自定義的配置節點(如果有用戶自定義的配置節點,則必須是configuration節點下的第一個節點)
?????????? 關于用戶自定義的配置節點的使用(待續)
轉載于:https://www.cnblogs.com/de0319gh/archive/2010/06/25/1765288.html
總結
以上是生活随笔為你收集整理的DotNet中配置文件的使用(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tmd烦死了什么梗 烦死了都郑爽表情包
- 下一篇: WAF对抗-安全狗(联合查询篇)