2024年4月26日发(作者:)

C#读写config配置文件

应用程序配置文件()是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。

对于一个config文件:

对config配置文件的读写类:

using System;

using c;

using ;

using ;

using rExpressions;

using uration;

using eModel;

using uration;

namespace NetUtilityLib

{

public static class ConfigHelper

{

//依据连接串名字connectionName返回数据连接字符串

public static string GetConnectionStringsConfig(string connectionName)

{

//指定config文件读取

string file = ablePath;

uration config = eConfiguration(file);

string connectionString =

tionStrings[connectionName].ng();

return connectionString;

}

///

///更新连接字符串

///

///连接字符串名称

///连接字符串内容

///数据提供程序名称

public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName)

{

//指定config文件读取

string file = ablePath;

Configuration config = eConfiguration(file);

bool exist = false; //记录该连接串是否已经存在

//如果要更改的连接串已经存在

if (tionStrings[newName] != null)

{

exist = true;

}

// 如果连接串已存在,首先删除它

if (exist)

{

(newName);

}

//新建一个连接字符串实例

ConnectionStringSettings mySettings =

new ConnectionStringSettings(newName, newConString, newProviderName);

// 将新的连接串添加到配置文件中.

(mySettings);

// 保存对配置文件所作的更改

(ed);

// 强制重新载入配置文件的ConnectionStrings配置节

hSection("ConnectionStrings");

}

///

///返回*.文件中appSettings配置节的value项

///

///

///

public static string GetAppConfig(string strKey)

{

string file = ablePath;

Configuration config = eConfiguration(file);

foreach (string key in s)

{

if (key == strKey)

{

return gs[strKey].ng();

}

}

return null;

}

///

///在*.文件中appSettings配置节增加一对键值对

///

///

///

public static void UpdateAppConfig(string newKey, string newValue)

{

string file = ablePath;

Configuration config = eConfiguration(file);

bool exist = false;

foreach (string key in s)

{

if (key == newKey)

{

exist = true;

}

}

if (exist)

{

(newKey);

}

(newKey, newValue);

(ed);

hSection("appSettings");

}

// 修改eModel下所有服务终结点的IP地址

public static void UpdateServiceModelConfig(string configPath, string serverIP)

{

Configuration config = eConfiguration(configPath);

ConfigurationSectionGroup sec = nGroups["eModel"];

ServiceModelSectionGroup serviceModelSectionGroup = sec as ServiceModelSectionGroup;

ClientSection clientSection = ;

foreach (ChannelEndpointElement item in nts)

{

string pattern = @"bd{1,3}.d{1,3}.d{1,3}.d{1,3}b";

string address = ng();

string replacement = ("{0}", serverIP);

address = e(address, pattern, replacement);

s = new Uri(address);

}

(ed);

hSection("eModel");

}

// 修改applicationSettings中gs中服务的IP地址

public static void UpdateConfig(string configPath, string serverIP)

{

Configuration config = eConfiguration(configPath);

ConfigurationSectionGroup sec = nGroups["applicationSettings"];

ConfigurationSection configSection = ns["gs"];

ClientSettingsSection clientSettingsSection = configSection as ClientSettingsSection;

if (clientSettingsSection != null)

{

SettingElement element1 =

("DataService_SystemManagerWS_SystemManagerWS");

if (element1 != null)

{

(element1);

string oldValue = ml;

ml = GetNewIP(oldValue, serverIP);

(element1);

}

SettingElement element2 =

("DataService_EquipManagerWS_EquipManagerWS");

if (element2 != null)

{

(element2);

string oldValue = ml;

ml = GetNewIP(oldValue, serverIP);

(element2);

}

}

(ed);

hSection("applicationSettings");

}

private static string GetNewIP(string oldValue, string serverIP)

{

string pattern = @"bd{1,3}.d{1,3}.d{1,3}.d{1,3}b";

string replacement = ("{0}", serverIP);

string newvalue = e(oldValue, pattern, replacement);

return newvalue;

}

}

}

测试代码如下:

class Program

{

static void Main(string[] args)

{

try

{

//string file = ablePath + ".config";

//string file1 = urationFile;

string serverIP = Config("ServerIP");

string db = Config("DataBase");

string user = Config("user");

string password = Config("password");

ine(serverIP);

ine(db);

ine(user);

ine(password);

AppConfig("ServerIP", "192.168.1.11");

string newIP = Config("ServerIP");

ine(newIP);

y();

}

catch (Exception ex)

{

ine(e);

}

}

}