如何用c#读写配置文件(转自:http://blog.csdn.net/lanman/article/details/5287717)
2011-07-26 20:56
633 查看
读配置很简单,可以用ConfigurationManager.AppSettings[key] 来读出,
可是写配置文件时,如果写成这样
ConfigurationManager.AppSettings[key] = "111";
总是提示只读,那么该怎么办呢?
view plainusing 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中。
分享到: 上一篇:SQL日期格式化应用大全
下一篇:Web服务配置错误: allowDefinition='MachineToApplication' 错误和 客户端发现响应内容类型为“text/html; charset=utf-8”...
可是写配置文件时,如果写成这样
ConfigurationManager.AppSettings[key] = "111";
总是提示只读,那么该怎么办呢?
view plainusing 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中。
分享到: 上一篇:SQL日期格式化应用大全
下一篇:Web服务配置错误: allowDefinition='MachineToApplication' 错误和 客户端发现响应内容类型为“text/html; charset=utf-8”...
相关文章推荐
- 用C#读写ini配置文件(转自:http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html)
- 如何用c#读写配置文件
- 如何用c#读写配置文件
- 如何用C#读写配置文件(转载自网络)
- 详解C#如何读写config配置文件
- 如何用c#读写配置文件,最后一句帮了我大忙!
- C#编程如何编写的自动读写配置文件.ini格式
- 【asp】如何打开asp文件,包括IIS安装,HTTP错误403.14,配置默认文档,启用目录浏览
- 自定义java.policy配置(如何让你的类禁止读写文件?禁止创建Socket对象?)
- C#如何调用其他.config配置文件,就是2个乃至3个以上的config文件
- C# 读写配置文件的简单类
- 在C#.NET中如何读写INI文件 from:http://www.linuxmine.com/48287.html
- C#读写配置文件(.config)(二)
- c#读写配置文件
- C#如何在控制台应用程序中加入配置文件
- C#中读写INI配置文件
- C#读写INI配置文件
- C# 读写App.config配置文件的方法
- 在C#中读写INI配置文件(转)
- 在C#中如何读取配置文件