asp.net2.0新添加了對web.config直接操做的功能。開發的時候有可能用到在web.config裏設置配置文件,實際上是能夠經過程序來設置這些配置節的。web
asp.net2.0須要添加引用:app
using System.Web.Configuration;asp.net
web.config裏的配置節:post
<appSettings>
<add key="FilePath" value="g:\Test\WebConfigManager\Upload\" />
<add key="p" value="g:\" />
</appSettings>
(1)讀spa
string filepath = ConfigurationManager.AppSettings["FilePath"];
(2)添加
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
AppSettingsSection app = config.AppSettings;
app.Settings.Add("p", "p:\\");
config.Save(ConfigurationSaveMode.Modified);
(3)修改
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
AppSettingsSection app = config.AppSettings;
app.Settings["p"].Value = @"g:\";
config.Save(ConfigurationSaveMode.Modified);
(4)刪除
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("p");
config.Save(ConfigurationSaveMode.Modified);
注意:
(1)asp.net用戶須要有讀取、修改、寫入的權限。
(2)
WebConfigManager是web.config所在的文件夾名。