日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DotNet中配置文件的使用(一)

發布時間:2023/12/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中配置文件的使用(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。