ASP.NET(C#)应用程序配置文件app.config/web.config的增、删、改操作
程序员文章站
2024-03-11 14:12:07
配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.net 2.0 开始,就非常方便了,提供了 system [.web] .con...
配置文件,对于程序本身来说,就是基础和依据,其本质是一个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);
对于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);