一个简单的读取appconfig的类

 
   

///
<summary>
/// 接点读取类
/// (一个简单的读取appconfig的类)
/// </summary>
public static class RConfig
{
/// <summary>
/// 读取接点到字符串
/// </summary>
/// <param name="key">key</param>
/// <returns></returns>
public static string ReadString(string key)
{
return ConfigurationManager.AppSettings[key];
}

/// <summary>
/// 读取一个整数
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int ReadInt(string key)
{
string val = ReadString(key);

if (string.IsNullOrEmpty(val))
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
else
{
int p;

if (int.TryParse(val, out p))
{
return p;
}
else
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
}
}


/// <summary>
/// 读取一个浮点数
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static float Readfloat(string key)
{
string val = ReadString(key);

if (string.IsNullOrEmpty(val))
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
else
{
float p;

if (float.TryParse(val, out p))
{
return p;
}
else
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
}
}


/// <summary>
/// 读取一个长整型
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static long ReadfLong(string key)
{
string val = ReadString(key);

if (string.IsNullOrEmpty(val))
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
else
{
long p;

if (long.TryParse(val, out p))
{
return p;
}
else
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
}
}


/// <summary>
/// 读取一个布尔值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Readfbool(string key)
{
string val = ReadString(key);

if (string.IsNullOrEmpty(val))
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
else
{
bool p;

if (bool.TryParse(val, out p))
{
return p;
}
else
{
throw new ConfigurationErrorsException(string.Format("节点{0}为空", key));
}
}
}

}


配置文件:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="WebConn" connectionString="server=**;database=**;uid=*;pwd=*"/>
</connectionStrings>
<appSettings>
<add key="Host" value="any"/>
<!--IP或HOST any表示绑定所有本地IP-->
<add key="Port" value="11223"/>
<!--端口-->
<add key="MaxBufferSize" value="1024"/>
<!--数据包最大缓冲区-->
<add key="MaxConnectCout" value="5000"/>
<!--最大连接数,按实际情况而设,太大内存占用太多,太小连接不够用 默认5000,最大可以设置 10万-->
<add key="MaxBuffSize" value="4096"/>
</appSettings>
</configuration>


 

 

 

 

你可能感兴趣的:(config)