如何用C#读写配置文件(转载自网络)
2016-09-21 09:47
363 查看
读配置很简单,可以用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;
}
}
}
可是写配置文件时,如果写成这样
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;
}
}
}
相关文章推荐
- 【转载】C#.NET 读写 INI配置文件
- 详解C#如何读写config配置文件
- 如何用c#读写配置文件
- 如何用c#读写配置文件
- 如何用c#读写配置文件,最后一句帮了我大忙!
- C#编程如何编写的自动读写配置文件.ini格式
- [转载]C#读写配置文件(XML文件)
- 如何用c#读写配置文件(转自:http://blog.csdn.net/lanman/article/details/5287717)
- 一个用C#编写的自动读写配置文件的开源组件
- 转载:C#实现对Word文件读写
- c#如何读取配置文件INI
- 一个用C#编写的自动读写配置文件的开源组件
- C#里如何读取和修改配置文件的值
- ArcIMS 投影问题 如何向地图配置文件中添加投影信息(转载)
- 转载:如何使用XCodeFactory自动生成XML配置文件和对应的解析类
- [转]用C#读写ini配置文件
- 一个用C#编写的自动读写配置文件的开源组件
- [转载][转]如何处理ASP.NET 2.0配置文件
- 一个用C#编写的自动读写配置文件的开源组件--V1.0.2版本
- VS2005中读写配置文件(方法一)(转载)