public class ConfigUtils
{
public static string filename = System.Windows.Forms.Application.StartupPath + @"\App.config";
/// <summary>
/// 对[appSettings]节点依据Key值读取到Value值,返回字符串
/// </summary>
/// <param name="key">要读取的Key值</param>
/// <returns>返回Value值的字符串</returns>
public static string GetAppSetting(string key)
{
string value = null;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
value = element.GetAttribute("value");
}
return value;
}
/// <summary>
/// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串
/// </summary>
/// <param name="name">要读取的name值</param>
/// <returns>返回connectionString值的字符串</returns>
public static string GetConnectionString(string name)
{
string connectionString = null;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
connectionString = element.GetAttribute("connectionString");
}
return connectionString;
}
/// <summary>
/// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="key">子节点Key值</param>
/// <param name="value">子节点value值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool SetAppSetting(string key, string value)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings");
try
{
if (node == null)
{
//不存在则新增appSettings子节点
XmlNode root = doc.DocumentElement;
XmlElement appElement = doc.CreateElement("appSettings");
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("key", key);
subElement.SetAttribute("value", value);
appElement.AppendChild(subElement);
root.AppendChild(appElement);
}
else
{
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
//存在则更新子节点Value
element.SetAttribute("value", value);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("key", key);
subElement.SetAttribute("value", value);
node.AppendChild(subElement);
}
}
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="name">子节点name值</param>
/// <param name="connectionString">子节点connectionString值</param>
/// <param name="providerName">子节点providerName值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool SetConnectionString(string name, string connectionString, string providerName)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
try
{
if (node == null)
{
//不存在则新增connectionStrings子节点
XmlNode root = doc.DocumentElement;
XmlElement connElement = doc.CreateElement("connectionStrings");
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
subElement.SetAttribute("providerName", providerName);
connElement.AppendChild(subElement);
root.AppendChild(connElement);
}
else
{
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
//存在则更新子节点
element.SetAttribute("connectionString", connectionString);
element.SetAttribute("providerName", providerName);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
subElement.SetAttribute("providerName", providerName);
node.AppendChild(subElement);
}
}
doc.Save(filename);
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="name">子节点name值</param>
/// <param name="connectionString">子节点connectionString值</param>
/// <param name="providerName">子节点providerName值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool SetConnectionString(string name, string connectionString)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
try
{
if (node == null)
{
//不存在则新增connectionStrings子节点
XmlNode root = doc.DocumentElement;
XmlElement connElement = doc.CreateElement("connectionStrings");
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
connElement.AppendChild(subElement);
root.AppendChild(connElement);
}
else
{
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
//存在则更新子节点
element.SetAttribute("connectionString", connectionString);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
node.AppendChild(subElement);
}
}
doc.Save(filename);
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
} /// <summary>
/// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="key">要删除的子节点Key值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteAppSetting(string key)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
//存在则删除子节点
element.ParentNode.RemoveChild(element);
}
try
{
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="name">要删除的子节点name值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteConnectionString(string name)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
//存在则删除子节点
node.RemoveChild(element);
} try
{
doc.Save(filename);
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
}
}
    class SetConfig
{
/// <summary>
/// 对[appSettings]节点依据Key值读取到Value值
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
public static string GetAppSettings(string strKey)
{
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == strKey)
{
return ConfigurationManager.AppSettings[strKey];
}
}
return null;
}
/// <summary>
/// 更新或新增[appSettings]节点的子节点值
/// </summary>
/// <param name="newKey"></param>
/// <param name="newValue"></param>
public static void SetAppSettings(string newKey, string newValue)
{
bool isModified = false;
// 如果要更改的Key已经存在
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == newKey)
{
isModified = true;
}
}
//打开 App.Config
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 如果连接串已存在,首先删除它
if (isModified)
{
config.AppSettings.Settings.Remove(newKey);
}
//添加新的配置到配置文件里
config.AppSettings.Settings.Add(newKey, newValue);
//保存对配置节所做的更改
config.Save(ConfigurationSaveMode.Modified);
//强制重载配置节
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 对[connectionStrings]节点依据name值读取
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
public static string GetConnectionString(string strName)
{
foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
{
if (item.Name == strName)
{
return ConfigurationManager.ConnectionStrings[strName].ConnectionString;
}
}
return null;
}
/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值
/// </summary>
/// <param name="newName"></param>
/// <param name="newValue"></param>
public static void SetConnectionString(string newName, string newValue)
{
bool isModified = false;
// 如果要更改的Key已经存在
foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
{
if (item.Name == newName)
{
isModified = true;
}
}
//打开 App.Config
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 如果连接串已存在,首先删除它
if (isModified)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//添加新的配置到配置文件里
ConnectionStringSettings conSetting = new ConnectionStringSettings(newName, newValue);
config.ConnectionStrings.ConnectionStrings.Add(conSetting);
//保存对配置节所做的更改
config.Save(ConfigurationSaveMode.Modified);
//强制重载配置节
ConfigurationManager.RefreshSection("connectionStrings");
} }

App.Config操作的更多相关文章

  1. c# 配置文件App.config操作类库

    public class ConfigOperator { #region 从配置文件获取Value /// <summary> /// 从配置文件获取Value /// </sum ...

  2. App.Config详解及读写操作

    App.Config详解及读写操作   App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...

  3. C#----操作应用程序配置文件App.config

    对配置文件的一些疑问: 在应用程序的目录下,有两处值得注意的地方,一个是应用程序根目录下的App.config文件,和bin\debug\name.exe.config 或者 bin\Release\ ...

  4. [转载]App.Config详解及读写操作

    App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...

  5. 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作

    最近我做的一些项目,经常需要用到对应用程序的配置文件操作,如app.config和web.config的配置文件,特别是对配置文件中的[appSettings]和[connectionStrings] ...

  6. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作

    原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...

  7. .NET下对Web.config与App.Config的增删改操作的代码

    把代码过程常用的内容做个收藏,下边代码段是关于 .NET下对Web.config与App.Config的增删改操作的代码. <?xml version="1.0" encod ...

  8. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  9. (转)App.Config详解及读写操作

    App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...

随机推荐

  1. Integer类的缓存机制

    一.Integer类的缓存机制 我们查看Integer的源码,就会发现里面有个静态内部类. public static Integer valueOf(int i) { assert IntegerC ...

  2. bui拍照上传、相册上传注意事项

    1.控制台输入 bui.currentPlatform  可查看工程项目基于什么平台  如:bingotouch 2.如果是 bingotouch , 在 index.js 或者其它配置的地方, 加上 ...

  3. C++数组读入MATLAB数据

    data = rand(8, 10); fid = fopen('File.data', 'w'); if fid == - 1 error('Cannot open file for writing ...

  4. JIRA之两大统计图讲解

    一.创建与解决的问题-状态统计图 配置方式 理解该统计图 横坐标 x:时间 纵坐标 y:issue数量 统计图示解读: A.随着时间的推移,创建的问题数(红线)减少,修复问题数(绿线)增加,标志着版本 ...

  5. spring security权限架架mvn坐标

    <!-- spring security start --> <dependency> <groupId>org.springframework.security& ...

  6. B/S大文件上传解决方案

    第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname =  ...

  7. Web上传文件的三种解决方案

    第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname =  ...

  8. Python_008(文件操作)

    一.文件操作 1.只读操作 f = open("taibai.txt",mode = "r",encoding = "utf-8" s = ...

  9. (转)Window 中杀死指定端口 cmd 命令行 taskkill

    Windows平台   两步方法 :  1 查询端口占用,2 强行杀死进程 netstat -aon|findstr "8080" taskkill /pid 4136-t -f ...

  10. 台哥原创:java 数独源码

    2010年,当时正在做手机游戏的客户端开发工作. 每天加班之余,用了两三个晚上,开发了这个数独. 主要是生成数独数组的算法,有点难度.. ​ 如下图:点选数字栏里的数字后,界面上所有该数字会高亮显示. ...