使用ConfigurationManager類(lèi) 讀寫(xiě)配置文件app.config,以下為代碼:
?
- using ?System;??
- using ?System.Configuration;??
- ??
- static ? class ?Program??
- ????{??
- ???????? static ? void ?Main()??
- ????????{??
- ????????????showConfig();??
- ????????????UpdateAppSettings();??
- ????????????showConfig();??
- ??
- ????????????Console.ReadKey( true );??
- ????????}??
- ??
- ???????? private ? static ? void ?showConfig()??
- ????????{??
- ???????????? string ??=?ConfigurationManager.AppSettings[ "Directory" ];??
- ????????????Console.WriteLine( "AppSetting配置節(jié)?Path?key的value為:" ?+?dir?+? "/n" );??
- ????????}??
- ??
- ???????? ///?<summary> ??
- ???????? ///?UpdateAppSettings? ??
- ???????? ///?</summary> ??
- ???????? public ? static ? void ?UpdateAppSettings()??
- ????????{??
- ???????????? //?Get?the?configuration?file.? ??
- ????????????Configuration?config?=?ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);??
- ????????????Console.WriteLine( "config.FIlePath:?" ?+?config.FilePath?+? "/n" );??
- ????????????config.AppSettings.Settings[ "Directory" ].Value?=? "tset" ;??
- ??
- ???????????? //?Save?the?configuration?file.? ??
- ????????????config.AppSettings.SectionInformation.ForceSave?=? true ;??
- ????????????config.Save(ConfigurationSaveMode.Modified);??
- ???????????? //?Force?a?reload?of?the?changed?section.? ??
- ????????????ConfigurationManager.RefreshSection( "appSettings" );??
- ????????}???
app.config內(nèi)容:
?
?
- <? xml ? version = "1.0" ? encoding = "utf-8" ? ?> ??
- < configuration > ??
- ?? < appSettings > ??
- ???? < add ? key = "Directory" ? value = "C:/Documents?and?Settings" /> ??
- ?? </ appSettings > ??
- </ configuration > ??
?
?
代碼結(jié)果:app.config只能作為初始化的定義,工程生成后運(yùn)行程序集名稱(chēng).exe 修改生成后的 程序集名稱(chēng).exe.Config文件
一開(kāi)始調(diào)試時(shí)看到控制結(jié)果是想要的結(jié)果,但看app.config配置文件內(nèi)容沒(méi)變(vs2008 F5調(diào)試模式下是修改 程序集名稱(chēng).vshost.exe.config配置文件)還以為是代碼有問(wèn)題,網(wǎng)上搜,也有人碰過(guò)到此現(xiàn)像,原來(lái)是自己沒(méi)有理解到MSDN的說(shuō)明。(還是有文化差異啊)
如:??????Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
??????????? Console.WriteLine("config.FIlePath: " + config.FilePath + "/n");
查看config.filePath值,即了解明白了。
?
4.0的類(lèi)庫(kù):? http://msdn.microsoft.com/en-us/library/ms134265(v=VS.100).aspx
?
注意:此類(lèi)是.net2.0后新增。
必須要先在工程里添加system.configuration.dll程序集的引用。
(在解決方案管理器中右鍵點(diǎn)擊工程名稱(chēng),在右鍵菜單中選擇添加引用,.net->組件名稱(chēng)->下即可找到)
添加引用后可以使用System.Configuration空間下的ConfigurationManager類(lèi).
?
引用資源:
http://www.ajaxline.com/node/258
http://www.codeproject.com/KB/cs/SystemConfiguration.aspx
http://www.cnblogs.com/interboy/archive/2007/04/22/722894.html
?????????? 使用ConfigurationManager類(lèi)讀寫(xiě)web.config
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
