色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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

配置文件,對于程序本身來說,就是基礎和依據,其本質是一個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);

AspNet技術ASP.NET(C#)應用程序配置文件app.config/web.config的增、刪、改操作,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 襄城县| 肇州县| 衡阳县| 瑞金市| 鄂尔多斯市| 荆州市| 黑龙江省| 垣曲县| 民县| 嫩江县| 南皮县| 长丰县| 舒兰市| 平果县| 六安市| 岱山县| 建水县| 时尚| 富顺县| 突泉县| 杂多县| 静乐县| 汉源县| 延长县| 枣庄市| 惠州市| 石渠县| 凤冈县| 莒南县| 来宾市| 闽侯县| 神池县| 宁夏| 壤塘县| 新田县| 闸北区| 原阳县| 肃北| 龙江县| 焦作市| 潜江市|