.net和c#下的自定义配置
1 在c#下自定义配置
1.1 配置文件下的配置 app.config不能更改名字
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dcsConfig" type="ConfigTest.Conf.DCSSection,ConfigTest"/>
</configSections>
<dcsConfig writeExceptionLog="1" maxConnectionNum="65535" buffSize="4024" reSendTimes="2" heartRate="1" socketOverTime="3">
<gateway>
<add name="dcsGW" domainName="www.abc.com" port="19100" protocolType="1"></add>
</gateway>
</dcsConfig>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
1.2 定义元素。集合类
class DCSSection : ConfigurationSection { public DCSSection() { } public DCSSection(string name) { Name = name; } [ConfigurationProperty("name")] public string Name { get { return this["name"].ToString(); } set { this["name"] = value; } } /// <summary> /// 网关配置. /// </summary> [ConfigurationProperty("gateway")] public GateWayCollection GateWays { get { return (GateWayCollection)base["gateway"]; } set { base["gateway"] = value; } } /// <summary> /// 写异常日志开关. /// </summary> /// <remarks>1 写日志 0 不写日志</remarks> [ConfigurationProperty("writeExceptionLog")] public int WriteExceptionLog { get { return (int)this["writeExceptionLog"]; } set { this["writeLog"] = value; } } /// <summary> /// 最大连接 /// </summary> /// <value>最大连接数.</value> [ConfigurationProperty("maxConnectionNum")] public int MaxConnectionNum { get { return (int)this["maxConnectionNum"]; } set { this["maxConnectionNum"] = value; } } /// <summary> /// 链路超时时间(单位min). /// </summary> [ConfigurationProperty("socketOverTime")] public int SocketOverTime { get { return (int)this["socketOverTime"]; } set { this["socketOverTime"] = value; } } /// <summary> /// 心跳频率(检测链路频率 单位s) /// </summary> [ConfigurationProperty("heartRate")] public int HeartRate { get { return (int)this["heartRate"]; } set { this["heartRate"] = value; } } /// <summary> /// 接收或发送缓存区大小(单位B ). /// </summary> [ConfigurationProperty("buffSize")] public int BuffSize { get { return (int)this["buffSize"]; } set { this["buffSize"] = value; } } /// <summary> /// 重传次数. /// </summary> [ConfigurationProperty("reSendTimes")] public int ReSendTimes { get { return (int)this["reSendTimes"]; } set { this["reSendTimes"] = value; } } internal static DCSSection GetConfigSection() { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //取得自定义配置区. DCSSection mySection = config.GetSection("dcsConfig") as DCSSection; return mySection; } }
1.3
public class GateWayElement:ConfigurationElement
{
public GateWayElement()
{
}
/// <summary>
/// 名称.
/// </summary>
[ConfigurationProperty("name")]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
/// <summary>
/// IP.
/// </summary>
/// <value></value>
[ConfigurationProperty("domainName")]
public string DomainName
{
get
{
return (string)base["domainName"];
}
set
{
base["domainName"] = value;
}
}
/// <summary>
/// port.
/// </summary>
/// <value>.</value>
[ConfigurationProperty("port")]
public int Port
{
get
{
return (int)this["port"];
}
set
{
this["port"] = value;
}
}
/// <summary>
/// 通信协议类型(TCP=1、UDP=2、TCP(SSL)=3)
/// </summary>
/// <value>.</value>
[ConfigurationProperty("protocolType")]
public int ProtocolType
{
get
{
return (int)this["protocolType"];
}
set
{
this["protocolType"] = value;
}
}
}
1.4
class GateWayCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMap;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new GateWayElement();
}
protected override Object GetElementKey(ConfigurationElement element)
{
return ((GateWayElement)element).Name;
}
public GateWayElement this[int index]
{
get
{
return (GateWayElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public GateWayElement this[string Name]
{
get
{
return (GateWayElement)BaseGet(Name);
}
}
public int IndexOf(GateWayElement url)
{
return BaseIndexOf(url);
}
public void Add(GateWayElement url)
{
BaseAdd(url);
}
protected override void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false);
}
public void Remove(GateWayElement url)
{
if (BaseIndexOf(url) >= 0)
BaseRemove(url.Name);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
public void Clear()
{
BaseClear();
}
}
1.4 调用
DCSSection var = DCSSection.GetConfigSection();
int a = var.HeartRate;
GateWayCollection list = var.GateWays;
foreach (GateWayElement item in list)
{
if (item.Name.ToLower().Trim() == "dcsgw")
{
MessageBox.Show(item.Port.ToString());
}
else if (item.Name.ToLower().Trim() == "domainName")
{
}
}
2 .net下的自定义配置
其他都一样有点不一样
//当不在独立 exe 内部运行时,必须指定 exePath。
internal static DCSSection GetConfigSection()
{
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string exePath = AppDomain.CurrentDomain.BaseDirectory + "Web.config";
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
//取得自定义配置区.
DCSSection mySection = config.GetSection("dcsConfig") as DCSSection;
return mySection;
}
}
}
// 可以建立个文件web.config.config,作为web.config的副本。
// 再次执行
// Configuration cfg = ConfigurationManager.OpenExeConfiguration(Server.MapPath("web.config"));
//cfg.AppSettings.Settings.Remove("123");
//cfg.AppSettings.Settings.Add("123","asdasd");
//Response.Write(cfg.FilePath);
//cfg.SaveAs(Server.MapPath("web.config"));
所以在在web.conf 下面在添加一个他的副本 web.config.config就可以读取了,(无奈之举)

浙公网安备 33010602011771号