很多时候我们需要对系统的.config文件进度读写操作,例如:系统初始化的参数的更改、系统参数的改变都需要更新到配置文件。

    首先我们有必要了解一下app.config、exe.config和vshost.exe.config作用和区别:

    vshost.exe.config是程序运行时的配置文本,exe.config是程序运行后会复制到vshost.exe.config,app.config是在vshost.exe.config和exe.config没有情况起作用,从app.config复制到exe.config再复制到vshost.exe.config。vshost.exe.config和exe.config会自动创建内容跟app.config一样。了解过这些其实写配置文件都是写到exe.config文件中了,app.config不会变化。网上也有许多关于配置文件的读写操作,也是借鉴了多位前辈的经验自己总结的一些比较常用的读写操作。废话不多说,直接上主题:

  1. appSetting节点

               /// <summary>
    /// 修改AppSettings中配置
    /// </summary>
    /// <param name="key">key值</param>
    /// <param name="value">相应值</param>
    public static bool SetConfigValue(string key, string value)
    {
    try
    {
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    if (config.AppSettings.Settings[key] != null)
    config.AppSettings.Settings[key].Value = value;
    else
    config.AppSettings.Settings.Add(key, value);
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    return true;
    }
    catch
    {
    return false;
    }
    }

    修改或新增AppSetting节点

             /// <summary>
    /// 获取AppSettings中某一节点值
    /// </summary>
    /// <param name="key"></param>
    public static string GetConfigValue(string key)
    {
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    if (config.AppSettings.Settings[key] != null)
    return config.AppSettings.Settings[key].Value;
    else return string.Empty;
    }

    获取AppSetting节点值

  2. ConnectionStrings节点

             /// <summary>
    /// 获取连接节点值
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetConnectionValue(string key)
    {
    if (ConfigurationManager.ConnectionStrings[key] != null)
    return ConfigurationManager.ConnectionStrings[key].ConnectionString;
    return string.Empty;
    }

    获取ConnectionStrings节点值  

     
      public static void UpdateConnectionStringsConfig(string key, string conString)
    {
    bool isModified = false; //记录该连接串是否已经存在
    if (ConfigurationManager.ConnectionStrings[key] != null)
    {
    isModified = true;
    }
    //新建一个连接字符串实例
    ConnectionStringSettings mySettings = new ConnectionStringSettings(key, conString); // 打开可执行的配置文件*.exe.config
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // 如果连接串已存在,首先删除它
    if (isModified)
    {
    config.ConnectionStrings.ConnectionStrings.Remove(key);
    }
    // 将新的连接串添加到配置文件中.
    config.ConnectionStrings.ConnectionStrings.Add(mySettings);
    // 保存对配置文件所作的更改
    config.Save(ConfigurationSaveMode.Modified);
    // 强制重新载入配置文件的ConnectionStrings配置节
    ConfigurationManager.RefreshSection("connectionStrings");
    }

    修改或新增ConnectionStrings节点

  3. System.ServiceModel节点

             /// <summary>
    /// 读取EndpointAddress
    /// </summary>
    /// <param name="endpointName"></param>
    /// <returns></returns>
    public static string GetEndpointClientAddress(string endpointName)
    {
    ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
    foreach (ChannelEndpointElement item in clientSection.Endpoints)
    {
    if (item.Name == endpointName)
    return item.Address.ToString();
    }
    return string.Empty;
    } /// <summary>
    /// 设置EndpointAddress
    /// </summary>
    /// <param name="endpointName"></param>
    /// <param name="address"></param>
    public static bool SetEndpointClientAddress(string endpointName, string address)
    {
    try
    {
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ClientSection clientSection = config.GetSection("system.serviceModel/client") as ClientSection;
    foreach (ChannelEndpointElement item in clientSection.Endpoints)
    {
    if (item.Name != endpointName)
    continue;
    item.Address = new Uri(address);
    break;
    }
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("system.serviceModel");
    return true;
    }
    catch (Exception ex)
    {
    return false;
    } }

    客户端Client的Endpoint

     
    服务端Service的Address

    对与配置文件的修改有些可能会觉得直接操作config文件对安全性来说代价太高了,这种情况下就需要个人取决一下可以使用将appconfig段放到独立的config文件中,以XML的方式进行修改,并可以避免应用程序重启的问题。

     简单的再说一下放到独立文件的操作

      

     剩下的就是对xml的操作

      

      

              string ConnectConfigPath = AppData.StartupPath + "\\Config\\DaoConfig.xml";//获取配置文件路径

                 //向DaoConfig里添加节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ConnectConfigPath);
XmlNode xmldocSelect = xmlDoc.SelectSingleNode("/DaoConfig[1]"); xmldocSelect.RemoveAll();//删除当前节点的所有子节点 //添加test节点
XmlElement Account = xmlDoc.CreateElement("test");
Account.InnerText = "对应的值";
xmldocSelect.AppendChild(Account); xmlDoc.Save(ConnectConfigPath);

Dao.config文件新增节点的操作

C# app.config文件配置和修改的更多相关文章

  1. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  2. 配置文件——App.config文件读取和修改

    作为普通的xml文件读取的话,首先就要知道怎么寻找文件的路径.我们知道一般配置文件就在跟可执行exe文件在同一目录下,且仅仅在名称后面添加了一个.config 因此,可以用Application.Ex ...

  3. Winform 数据库连接app.config文件配置 数据库连接字符串

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  4. Winform数据库连接app.config文件配置

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  5. c#Winform程序调用app.config文件配置数据库连接字符串

    你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings name="  " connectionString= ...

  6. C#中App.config文件配置获取

    最新的framework使用如下方法: using System.Configuration; ConfigurationManager.AppSettings["key"]; A ...

  7. C# App.config文件配置数据的读写

    添加程序集引用  System.configuration.dll 和命名空间 using System.Configuration; 读: ConfigurationManager.AppSetti ...

  8. 附带详细注释的log4net的app.config文件配置例子

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  9. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

随机推荐

  1. 90、 Android UI模板设计

    第一步:自定义xml属性 新建一个android项目,在values文件夹中新建一个atts.xml的文件,在这个xml文件中声明我们一会在使用自定义控件时候需要指明的属性.atts.xml < ...

  2. memcpy、memmove、memset

    void * memcpy(void * dst, const void * src, size_t count) { void *res=dst; while (count--) { *(char* ...

  3. Throttling ASP.NET Web API calls

    http://blog.maartenballiauw.be/post/2013/05/28/Throttling-ASPNET-Web-API-calls.aspx https://github.c ...

  4. Open XML SDK 在线编程黑客松

    2015年2月10日-3月20日,开源社 成员 微软开放技术,GitCafe,极客学院联合举办" Open XML SDK 在线编程黑客松 ",为专注于开发提高生产力的应用及服务的 ...

  5. iptable怎么用?

    iptables -A FORWARD -s 10.0.0.0/8  -p tcp  --dport  80  -j DROP [拒绝转发来自10.0.0.0/8网段,目的端口是80的数据包]

  6. SQL Server :Stored procedures存储过程初级篇

    对于SQL Server,我是个拿来主义.很多底层的原理并不了解,就直接模仿拿着来用了,到了报错的时候,才去找原因进而逐步深入底层.我想,是每一次的报错,逼着我一点点进步的吧. 近期由于项目的原因,我 ...

  7. Debian的一个命令

    dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包.下面是它的一些命令解释:1)安装软件命令行:dpkg -i <.deb file name> ...

  8. 使用Zabbix监控RabbitMQ

    一 应用场景描述 线上业务使用RabbitMQ作为消息队列中间件,那么作为运维人员对RabbitMQ的监控就很重要,本文就针对如何从头到尾使用Zabbix来监控RabbitMQ进行说明. 二 Rabb ...

  9. 在Service Fabric上部署Java应用,体验一把微服务的自动切换

    虽然Service Fabric的Java支持版本还没有正式发布,但是Service Fabric本身的服务管理.部署.升级等功能是非常好用的,那么Java的开发者可以如何利用上Service Fab ...

  10. 【LeetCode】21. Merge Two Sorted Lists

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...