zoukankan      html  css  js  c++  java
  • 读写appSettings配置节方法

    <configuration>
      <appSettings>
        <add key="Socket_Path_MW_data" value="D:MonitorSocketMWdata"/> 
      </appSettings>  


    添加System.Configuration.dll引用

    引用using System.Configuration名称空间


    读取config文件的appSettings节的方法比较简单,可以通过上文中  System.Configuration.ConfigurationManager.AppSettings["Socket_Path_MW_data"]  的方法进行访问,但该方法不提供写入。


    如果希望写入配置文件,可以使用ConfigurationManager对象执行打开配置文件的操作后,将会返回一个Configuration的对象,利用该对象进行操作(增删改查都可以)。


            private void AccessAppSettings()
            {
                //获取Configuration对象
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //根据Key读取<add>元素的Value
                string name = config.AppSettings.Settings["name"].Value;
                //写入<add>元素的Value
                config.AppSettings.Settings["name"].Value = "xiao";
                //增加<add>元素
                config.AppSettings.Settings.Add("url""http://www.baidu.com");
                //删除<add>元素
                config.AppSettings.Settings.Remove("name");
                //一定要记得保存,写不带参数的config.Save()也可以
                config.Save(ConfigurationSaveMode.Modified);
                //刷新,否则程序读取的还是之前的值(可能已装入内存)
                ConfigurationManager.RefreshSection("appSettings");
            }  




  • 相关阅读:
    SHELL种类,版本及选择
    delete
    ctrl+alt+l:linux 锁屏 win+l:windows锁屏
    inux关于readlink函数获取运行路径的小程序
    网络版shell之网络编程练习篇--telnet服务端
    CentOS 6.5配置nfs服务
    linux操作系下RAR的使用
    BLOB二进制对象(blob.c/h)
    循环队列
    java的System.getProperty()方法能够获取的值
  • 原文地址:https://www.cnblogs.com/Full--Stack/p/8041413.html
Copyright © 2011-2022 走看看