欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 读取并修改config文件实现代码

程序员文章站 2024-03-09 14:06:17
1. 向项目添加app.config文件: 右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”;如果项目以前没有配置文件...
1. 向项目添加app.config文件:
右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”;如果项目以前没有配置文件,则默认的文件名称为“app.config”,单击“确定”。出现在设计器视图中的app.config文件为:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
</configuration>
在项目进行编译后,在bin\debuge文件下,将出现两个配置文件(以本项目为例),一个名为“jxcmanagement.exe.config”,另一个名为“jxcmanagement.vshost.exe.config”。第一个文件为项目实际使用的配置文件,在程序运行中所做的更改都将被保存于此;第二个文件为原代码“app.config”的同步文件,在程序运行中不会发生更改。
2. connectionstrings配置节:
请注意:如果您的sql版本为2005 express版,则默认安装时sql服务器实例名为localhost\sqlexpress,须更改以下实例中“data source=localhost;”一句为“data source=localhost\sqlexpress;”,在等于号的两边不要加上空格。
<!--数据库连接串-->
复制代码 代码如下:

<connectionstrings>
<clear />
<addname="conjxcbook"
connectionstring="data source=localhost;initial catalog=jxcbook;user id=sa;password=********"
providername="system.data.sqlclient" />
</connectionstrings>

3. appsettings配置节:
appsettings配置节为整个程序的配置,如果是对当前用户的配置,请使用usersettings配置节,其格式与以下配置书写要求一样。
<!--进销存管理系统初始化需要的参数-->
复制代码 代码如下:

<appsettings>
<clear />
<addkey="username"value="" />
<addkey="password"value="" />
<addkey="department"value="" />
<addkey="returnvalue"value="" />
<addkey="pwdpattern"value="" />
<addkey="userpattern"value="" />
</appsettings>

4.读取与更新app.config
对于app.config文件的读写,参照了网络文章:http://www.codeproject.com/csharp/ systemconfiguration.asp标题为“read/write app.config file with .net 2.0”一文。
请注意:要使用以下的代码访问app.config文件,除添加引用system.configuration外,还必须在项目添加对system.configuration.dll的引用。
4.1 读取connectionstrings配置节
复制代码 代码如下:

///<summary>
///依据连接串名字connectionname返回数据连接字符串
///</summary>
///<param name="connectionname"></param>
///<returns></returns>
private static string getconnectionstringsconfig(string connectionname)
{
string connectionstring =
configurationmanager.connectionstrings[connectionname].connectionstring.tostring();
console.writeline(connectionstring);
return connectionstring;
}

4.2 更新connectionstrings配置节
复制代码 代码如下:

///<summary>
///更新连接字符串
///</summary>
///<param name="newname">连接字符串名称</param>
///<param name="newconstring">连接字符串内容</param>
///<param name="newprovidername">数据提供程序名称</param>
private static void updateconnectionstringsconfig(string newname,
string newconstring,
string newprovidername)
{
bool ismodified = false; //记录该连接串是否已经存在
//如果要更改的连接串已经存在
if (configurationmanager.connectionstrings[newname] != null)
{
ismodified = true;
}
//新建一个连接字符串实例
connectionstringsettings mysettings =
new connectionstringsettings(newname, newconstring, newprovidername);
// 打开可执行的配置文件*.exe.config
configuration config =
configurationmanager.openexeconfiguration(configurationuserlevel.none);
// 如果连接串已存在,首先删除它
if (ismodified)
{
config.connectionstrings.connectionstrings.remove(newname);
}
// 将新的连接串添加到配置文件中.
config.connectionstrings.connectionstrings.add(mysettings);
// 保存对配置文件所作的更改
config.save(configurationsavemode.modified);
// 强制重新载入配置文件的connectionstrings配置节
configurationmanager.refreshsection("connectionstrings");
}

4.3 读取appstrings配置节
复制代码 代码如下:

///<summary>
///返回*.exe.config文件中appsettings配置节的value项
///</summary>
///<param name="strkey"></param>
///<returns></returns>
private static string getappconfig(string strkey)
{
foreach (string key in configurationmanager.appsettings)
{
if (key == strkey)
{
return configurationmanager.appsettings[strkey];
}
}
return null;
}

4.4 更新connectionstrings配置节
复制代码 代码如下:

///<summary>
///在*.exe.config文件中appsettings配置节增加一对键、值对
///</summary>
///<param name="newkey"></param>
///<param name="newvalue"></param>
private static void updateappconfig(string newkey, string newvalue)
{
bool ismodified = false;
foreach (string key in configurationmanager.appsettings)
{
if(key==newkey)
{
ismodified = true;
}
}

// open app.config of executable
configuration config =
configurationmanager.openexeconfiguration(configurationuserlevel.none);
// you need to remove the old settings object before you can replace it
if (ismodified)
{
config.appsettings.settings.remove(newkey);
}
// add an application setting.
config.appsettings.settings.add(newkey,newvalue);
// save the changes in app.config file.
config.save(configurationsavemode.modified);
// force a reload of a changed section.
configurationmanager.refreshsection("appsettings");
}


复制代码 代码如下:

/// <summary>
/// 写入key,value 到xml文件
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void saveconfig(string key,string value)
{
xmldocument doc = new xmldocument();
//获得配置文件的全路径
string strfilename = appdomain.currentdomain.basedirectory.tostring() + "app.config";
doc.load(strfilename);
//找出名称为“add”的所有元素
xmlnodelist nodes = doc.getelementsbytagname("add");
for (int i = 0; i < nodes.count; i++)
{
//获得将当前元素的key属性
xmlattribute att = nodes[i].attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.value == key)
{
//对目标元素中的第二个属性赋值
att = nodes[i].attributes["value"];

att.value = value;
break;
}
}
//保存上面的修改
doc.save(strfilename);

}