/// <summary>
/// 获取自定义配置文件
/// </summary>
public class Config : ConfigurationSection
{
private Configuration configObject;
public const string ORACLE = "oracle";
public const string MYSQL = "mysql";
public const string SQLSERVER = "sqlserver";
/// <summary>
/// 数据库连接类型
/// </summary>
/// <param name="dataBaseType">oracle/mysql/sqlserver</param>
public Config(string dataBaseType)
{
dataBaseType = dataBaseType.ToLower();
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + "ConfigFiles\\" + dataBaseType + ".config";
configObject = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
}
/// <summary>
/// 得到对应键的值
/// </summary>
/// <param name="key">键名</param>
/// <returns></returns>
public string GetValue(string key)
{
string val = string.Empty;
if (configObject.AppSettings.Settings.AllKeys.Contains(key))
val = configObject.AppSettings.Settings[key].Value;
return val;
}
/// <summary>
/// 获取所有配置文件
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetConfig()
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (string key in configObject.AppSettings.Settings.AllKeys)
dict.Add(key, configObject.AppSettings.Settings[key].Value);
return dict;
}
/// <summary>
/// 根据键值获取配置文件
/// </summary>
/// <param name="key">键值</param>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public string GetConfig(string key, string defaultValue)
{
string val = string.Empty;
if (configObject.AppSettings.Settings.AllKeys.Contains(key))
{
val = configObject.AppSettings.Settings[key].Value;
}
if (string.IsNullOrEmpty(val))
{
return defaultValue;
}
return val;
}
/// <summary>
/// 写配置文件,如果节点不存在则自动创建
/// </summary>
/// <param name="key">键值</param>
/// <param name="value">值</param>
/// <returns></returns>
public bool SetConfig(string key, string value)
{
try
{
//Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (!configObject.AppSettings.Settings.AllKeys.Contains(key))
configObject.AppSettings.Settings.Add(key, value);
else
configObject.AppSettings.Settings[key].Value = value;
configObject.Save(ConfigurationSaveMode.Modified);
//ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch { return false; }
}
/// <summary>
/// 写配置文件(用键值创建),如果节点不存在则自动创建
/// </summary>
/// <param name="dict">键值集合</param>
/// <returns></returns>
public bool SetConfig(Dictionary<string, string> dict)
{
try
{
if (dict == null || dict.Count == 0)
return false;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (string key in dict.Keys)
{
if (!configObject.AppSettings.Settings.AllKeys.Contains(key))
configObject.AppSettings.Settings.Add(key, dict[key]);
else
configObject.AppSettings.Settings[key].Value = dict[key];
}
configObject.Save(ConfigurationSaveMode.Modified);
return true;
}
catch { return false; }
}
}
public class ItemsSection : ConfigurationSection
{
[ConfigurationProperty("items", IsRequired = true)]
public string Category
{
get
{
return (string)base["Category"];
}
set
{
base["Category"] = value;
}
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public ItemElementCollection Books
{
get
{
return (ItemElementCollection)base[""];
}
}
}
public class ItemElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ItemElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ItemElement)element).Name;
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "item";
}
}
public ItemElement this[int index]
{
get
{
return (ItemElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
}
public class ItemElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)base["name"];
}
set
{
base["name"] = value;
}
}
[ConfigurationProperty("icon", IsRequired = true)]
public string Icon
{
get
{
return (string)base["icon"];
}
set
{
base["icon"] = value;
}
}
[ConfigurationProperty("assembly", IsRequired = true)]
public string Assembly
{
get
{
return (string)base["assembly"];
}
set
{
base["assembly"] = value;
}
}
[ConfigurationProperty("target", IsRequired = true)]
public string Target
{
get
{
return (string)base["target"];
}
set
{
base["target"] = value;
}
}
[ConfigurationProperty("kind", IsRequired = true)]
public string Kind
{
get
{
return (string)base["kind"];
}
set
{
base["name"] = value;
}
}
}
C# 获取自定义配置文件
于 2023-04-20 09:27:22 首次发布