zoukankan      html  css  js  c++  java
  • c#读写配置文件

    读配置很简单,可以用ConfigurationManager.AppSettings[key] 来读出,

    可是写配置文件时,如果写成这样

    ConfigurationManager.AppSettings[key] = "111";

    总是提示只读,那么该怎么办呢?

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using System.Configuration;  
    5.   
    6. namespace BQKJ.Common  
    7. {  
    8.     /// <summary>  
    9.     /// 对exe.Config文件中的appSettings段进行读写配置操作  
    10.     /// 注意:调试时,写操作将写在vhost.exe.config文件中  
    11.     /// </summary>  
    12.     public class ConfigAppSettings  
    13.     {  
    14.         /// <summary>  
    15.         /// 写入值  
    16.         /// </summary>  
    17.         /// <param name="key"></param>  
    18.         /// <param name="value"></param>  
    19.         public static void SetValue(string key, string value)  
    20.         {  
    21.             //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>  
    22.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    23.             if (config.AppSettings.Settings[key] == null)  
    24.             {  
    25.                 config.AppSettings.Settings.Add(key, value);  
    26.             }  
    27.             else  
    28.             {  
    29.                 config.AppSettings.Settings[key].Value = value;  
    30.             }  
    31.             config.Save(ConfigurationSaveMode.Modified);  
    32.             ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件   
    33.         }  
    34.   
    35.         /// <summary>  
    36.         /// 读取指定key的值  
    37.         /// </summary>  
    38.         /// <param name="key"></param>  
    39.         /// <returns></returns>  
    40.         public static string GetValue(string key)  
    41.         {   
    42.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    43.             if (config.AppSettings.Settings[key] == null)  
    44.                 return "";  
    45.             else  
    46.                 return config.AppSettings.Settings[key].Value;  
    47.         }  
    48.   
    49.     }  
    50. }     

    其实也很简单,用这两个封装过的方法就可以了。

    需要注意的是,在IDE调试时,写入的配置文件其实是写在了.vshost.exe.config文件中,所以你在.exe.config中是看不到的。只有直接运行exe文件时,才会正确写入到.exe.config中。

     private void button1_Click(object sender, EventArgs e)

     {

                string serverIP = ConfigHelper.GetValue("ServerIP");

                string db = ConfigHelper.GetValue("DataBase");

                string user = ConfigHelper.GetValue("user");

                string password = ConfigHelper.GetValue("password");

                string info = "serverIP:" + serverIP + " "

                + "db:" + db + " "

                + "user:" + user + " "

                + "password:" + password + " ";

                MessageBox.Show(info);

                ConfigHelper.SetValue("DataBase", "ttdb");

                string newIP = ConfigHelper.GetValue("DataBase");

                MessageBox.Show(newIP);

        }

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using System.Configuration;  
    5.   
    6. namespace BQKJ.Common  
    7. {  
    8.     /// <summary>  
    9.     /// 对exe.Config文件中的appSettings段进行读写配置操作  
    10.     /// 注意:调试时,写操作将写在vhost.exe.config文件中  
    11.     /// </summary>  
    12.     public class ConfigAppSettings  
    13.     {  
    14.         /// <summary>  
    15.         /// 写入值  
    16.         /// </summary>  
    17.         /// <param name="key"></param>  
    18.         /// <param name="value"></param>  
    19.         public static void SetValue(string key, string value)  
    20.         {  
    21.             //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>  
    22.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    23.             if (config.AppSettings.Settings[key] == null)  
    24.             {  
    25.                 config.AppSettings.Settings.Add(key, value);  
    26.             }  
    27.             else  
    28.             {  
    29.                 config.AppSettings.Settings[key].Value = value;  
    30.             }  
    31.             config.Save(ConfigurationSaveMode.Modified);  
    32.             ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件   
    33.         }  
    34.   
    35.         /// <summary>  
    36.         /// 读取指定key的值  
    37.         /// </summary>  
    38.         /// <param name="key"></param>  
    39.         /// <returns></returns>  
    40.         public static string GetValue(string key)  
    41.         {   
    42.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    43.             if (config.AppSettings.Settings[key] == null)  
    44.                 return "";  
    45.             else  
    46.                 return config.AppSettings.Settings[key].Value;  
    47.         }  
    48.   
    49.     }  
    50. }  
  • 相关阅读:
    ASP.NET AJAX 1.0 英文文档,视频教程
    第十五篇: Ajax Control Toolkit 控件包3. DragPanel (拖动效果)
    第二篇: Silverlight 下载与安装
    第十二篇: Ajax Control Toolkit 控件包1. Accordion (多层折叠效果)
    第一篇: Silverlight 效果展示
    第十四篇: 建立 AJAX 母版页 (为了后面例子方便)
    第三篇: Silverlight 2.0 下载与安装
    第十三篇: Ajax Control Toolkit 控件包2. CollapsiblePanel (展开和折叠效果)
    第十六篇: Ajax Control Toolkit 控件包4. DropShadow (阴影效果)
    ENDNOTE使用方法(转发)
  • 原文地址:https://www.cnblogs.com/lgx5/p/7353690.html
Copyright © 2011-2022 走看看