国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET(C#)應用程序配置文件app.config/web.config的增、刪、改操作

2024-07-10 13:19:50
字體:
來源:轉載
供稿:網友
配置文件,對于程序本身來說,就是基礎和依據,其本質是一個xml文件,對于配置文件的操作,從.NET 2.0 開始,就非常方便了,提供了 System [.Web] .Configuration 這個管理功能的NameSpace,要使用它,需要添加對 System.configuration.dll的引用。
對于WINFORM程序,使用 System.Configuration.ConfigurationManager;
對于ASP.NET 程序, 使用 System.Web.Configuration.WebConfigurationManager;
對于配置文件內容的讀取,真是太普遍不過了,如果你的程序里,沒有讀取配置文件內容的方面,你都不好意思拿出來用
我們以最常見的 AppSettings 小節來作為例子:
假設有如下的配置文件內容:

復制代碼 代碼如下:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="y" value="this is Y"/>
</appSettings>
</configuration>


1. 讀取值:
* Asp.Net: System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];
* WinForm: System.Configuration.ConfigurationManager.AppSettings[“y”];
2. 添加一項
ASP.NET(需要有寫權限):
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
WinForm:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
3. 修改一項
* Asp.Net
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
//app.Settings.Add("x", "this is X");
app.Settings["x"].Value = "this is not Y";
config.Save(ConfigurationSaveMode.Modified);
* WinForm
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
//app.Settings.Add("x", "this is X");
app.Settings["x"].Value = "this is not Y";
config.Save(ConfigurationSaveMode.Modified);
4. 刪除一項
* Asp.Net
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("x");
config.Save(ConfigurationSaveMode.Modified);
* WinForm
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("x");
config.Save(ConfigurationSaveMode.Modified);
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 子长县| 信丰县| 信丰县| 阿拉善右旗| 离岛区| 措勤县| 余干县| 新巴尔虎左旗| 军事| 友谊县| 兴义市| 旺苍县| 城市| 天峨县| 太仓市| 洛南县| 伊宁市| 洛隆县| 江阴市| 济源市| 固始县| 宜章县| 连城县| 金阳县| 湘潭市| 连云港市| 吉木乃县| 申扎县| 嵊泗县| 襄城县| 仁寿县| 虎林市| 忻城县| 泰兴市| 德格县| 扶沟县| 梨树县| 荣成市| 堆龙德庆县| 化州市| 天祝|