讀配置很簡單,可以用ConfigurationManager.AppSettings[key] 來讀出,
可是寫配置文件時,如果寫成這樣
ConfigurationManager.AppSettings[key] = "111";
總是提示只讀,那么該怎么辦呢?
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Configuration;
- namespace BQKJ.Common
- {
- /// <summary>
- /// 對exe.Config文件中的appSettings段進行讀寫配置操作
- /// 注意:調試時,寫操作將寫在vhost.exe.config文件中
- /// </summary>
- public class ConfigAppSettings
- {
- /// <summary>
- /// 寫入值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void SetValue(string key, string value)
- {
- //增加的內容寫在appSettings段下 <add key="RegCode" value="0"/>
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- {
- config.AppSettings.Settings.Add(key, value);
- }
- else
- {
- config.AppSettings.Settings[key].Value = value;
- }
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加載新的配置文件
- }
- /// <summary>
- /// 讀取指定key的值
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string GetValue(string key)
- {
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- return "";
- else
- return config.AppSettings.Settings[key].Value;
- }
- }
- }
其實也很簡單,用這兩個封裝過的方法就可以了。
需要注意的是,在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 + "\r\n"
+ "db:" + db + "\r\n"
+ "user:" + user + "\r\n"
+ "password:" + password + "\r\n";
MessageBox.Show(info);
ConfigHelper.SetValue("DataBase", "ttdb");
string newIP = ConfigHelper.GetValue("DataBase");
MessageBox.Show(newIP);
}
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Configuration;
- namespace BQKJ.Common
- {
- /// <summary>
- /// 對exe.Config文件中的appSettings段進行讀寫配置操作
- /// 注意:調試時,寫操作將寫在vhost.exe.config文件中
- /// </summary>
- public class ConfigAppSettings
- {
- /// <summary>
- /// 寫入值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void SetValue(string key, string value)
- {
- //增加的內容寫在appSettings段下 <add key="RegCode" value="0"/>
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- {
- config.AppSettings.Settings.Add(key, value);
- }
- else
- {
- config.AppSettings.Settings[key].Value = value;
- }
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加載新的配置文件
- }
- /// <summary>
- /// 讀取指定key的值
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string GetValue(string key)
- {
- System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (config.AppSettings.Settings[key] == null)
- return "";
- else
- return config.AppSettings.Settings[key].Value;
- }
- }
- }