C# 操作配置文件 App.config的详解
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace Schwann.CommLibrary
{
public class ConfigHelper
{
///
/// 根据键值获取配置文件
///
/// 键值
///
public static string GetConfig(string key)
{
string val = string.Empty;
if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
val = ConfigurationManager.AppSettings[key];
return val;
}
///
/// 获取所有配置文件
///
///
public static Dictionary GetConfig()
{
Dictionary dict = new Dictionary();
foreach (string key in ConfigurationManager.AppSettings.AllKeys)
dict.Add(key, ConfigurationManager.AppSettings[key]);
return dict;
}
///
/// 根据键值获取配置文件
///
/// 键值
/// 默认值
///
public static string GetConfig(string key, string defaultValue)
{
string val = defaultValue;
if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
val = ConfigurationManager.AppSettings[key];
if (val == null)
val = defaultValue;
return val;
}
///
/// 写配置文件,如果节点不存在则自动创建
///
/// 键值
/// 值
///
public static bool SetConfig(string key, string value)
{
try
{
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (!conf.AppSettings.Settings.AllKeys.Contains(key))
conf.AppSettings.Settings.Add(key, value);
else
conf.AppSettings.Settings[key].Value = value;
conf.Save();
return true;
}
catch { return false; }
}
///
/// 写配置文件(用键值创建),如果节点不存在则自动创建
///
/// 键值集合
///
public static bool SetConfig(Dictionary dict)
{
try
{
if (dict == null || dict.Count == 0)
return false;
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (string key in dict.Keys)
{
if (!conf.AppSettings.Settings.AllKeys.Contains(key))
conf.AppSettings.Settings.Add(key, dict[key]);
else
conf.AppSettings.Settings[key].Value = dict[key];
}
conf.Save();
return true;
}
catch { return false; }
}
}
} 上一篇学习了配置文件读取的处理方式,但是没有对经常用到的
假设有如下配置参数
读取:
string address=System.Configuration.ConfigurationManager.AppSettings["address"].ToString();
事实就是这么简单
以上就是c# 操作配置文件 app.config的详解的内容,更多相关内容请关注php中文网(www.php.cn)!
方科网络ERP图文店II版为仿代码站独立研发的网络版ERP销售程序。本本版本为方科网络ERP图文店版的简化版,去除了部分不同用的功能,使得系统更加精炼实用。考虑到图文店的特殊情况,本系统并未制作出入库功能,而是将销售作为重头,使用本系统,可以有效解决大型图文店员工多,换班数量多,订单混杂不清的情况。下单、取件、结算分别记录操作人员,真正做到订单全程跟踪!无限用户级别,不同的用户级别可以设置不同的价









