获取真实基础配置对象
BaseConfigProvider.cs
/**/
/// <summary>
/// 获取真实基础配置对象
/// </summary>
/// <returns></returns>
public static BaseConfigInfo GetRealBaseConfig()
... {
BaseConfigInfo newBaseConfig = null;
string filename = null;
HttpContext context = HttpContext.Current;
if (context != null)
filename = context.Server.MapPath("/BwConf.config");
else
filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BwConf.config");
try
...{
newBaseConfig = (BaseConfigInfo)SerializationHelper.Load(typeof(BaseConfigInfo), filename);
}
catch
...{
newBaseConfig = null;
}
if (newBaseConfig == null)
...{
try
...{
BaseConfigInfoCollection bcc = (BaseConfigInfoCollection)SerializationHelper.Load(typeof(BaseConfigInfoCollection), filename);
foreach (BaseConfigInfo bc in bcc)
...{
if (Utils.GetTrueForumPath() == bc.Forumpath)
...{
newBaseConfig = bc;
break;
}
}
if (newBaseConfig == null)
...{
BaseConfigInfo rootConfig = null;
foreach (BaseConfigInfo bc in bcc)
...{
if (Utils.GetTrueForumPath().StartsWith(bc.Forumpath) && bc.Forumpath != "/")
...{
newBaseConfig = bc;
break;
}
if (("/").Equals(bc.Forumpath))
...{
rootConfig = bc;
}
}
if (newBaseConfig == null)
...{
newBaseConfig = rootConfig;
}
}
}
catch
...{
newBaseConfig = null;
}
}
if (newBaseConfig == null)
...{
filename = context.Server.MapPath("~") + "/BwConf.config";
try
...{
newBaseConfig = (BaseConfigInfo)SerializationHelper.Load(typeof(BaseConfigInfo), filename);
}
catch
...{
newBaseConfig = null;
}
}
if (newBaseConfig == null)
...{
throw new Exception("发生错误: 网站根目录下没有正确的BwConf.config文件");
}
return newBaseConfig;
}
/// 获取真实基础配置对象
/// </summary>
/// <returns></returns>
public static BaseConfigInfo GetRealBaseConfig()
... {
BaseConfigInfo newBaseConfig = null;
string filename = null;
HttpContext context = HttpContext.Current;
if (context != null)
filename = context.Server.MapPath("/BwConf.config");
else
filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BwConf.config");
try
...{
newBaseConfig = (BaseConfigInfo)SerializationHelper.Load(typeof(BaseConfigInfo), filename);
}
catch
...{
newBaseConfig = null;
}
if (newBaseConfig == null)
...{
try
...{
BaseConfigInfoCollection bcc = (BaseConfigInfoCollection)SerializationHelper.Load(typeof(BaseConfigInfoCollection), filename);
foreach (BaseConfigInfo bc in bcc)
...{
if (Utils.GetTrueForumPath() == bc.Forumpath)
...{
newBaseConfig = bc;
break;
}
}
if (newBaseConfig == null)
...{
BaseConfigInfo rootConfig = null;
foreach (BaseConfigInfo bc in bcc)
...{
if (Utils.GetTrueForumPath().StartsWith(bc.Forumpath) && bc.Forumpath != "/")
...{
newBaseConfig = bc;
break;
}
if (("/").Equals(bc.Forumpath))
...{
rootConfig = bc;
}
}
if (newBaseConfig == null)
...{
newBaseConfig = rootConfig;
}
}
}
catch
...{
newBaseConfig = null;
}
}
if (newBaseConfig == null)
...{
filename = context.Server.MapPath("~") + "/BwConf.config";
try
...{
newBaseConfig = (BaseConfigInfo)SerializationHelper.Load(typeof(BaseConfigInfo), filename);
}
catch
...{
newBaseConfig = null;
}
}
if (newBaseConfig == null)
...{
throw new Exception("发生错误: 网站根目录下没有正确的BwConf.config文件");
}
return newBaseConfig;
}
SerializationHelper
/**/
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static object Load(Type type, string filename)
... {
FileStream fs = null;
try
...{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch(Exception ex)
...{
throw ex;
}
finally
...{
if(fs != null)
fs.Close();
}
}
/**/ /// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static void Save( object obj, string filename)
... {
FileStream fs = null;
// serialize it...
try
...{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
}
catch(Exception ex)
...{
throw ex;
}
finally
...{
if(fs != null)
fs.Close();
}
}
/// 反序列化
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static object Load(Type type, string filename)
... {
FileStream fs = null;
try
...{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch(Exception ex)
...{
throw ex;
}
finally
...{
if(fs != null)
fs.Close();
}
}
/**/ /// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static void Save( object obj, string filename)
... {
FileStream fs = null;
// serialize it...
try
...{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
}
catch(Exception ex)
...{
throw ex;
}
finally
...{
if(fs != null)
fs.Close();
}
}
基本设置描述类, 加[Serializable]标记为可序列化
[Serializable]
public class BaseConfigInfo : IConfigInfo
... {
私有字段#region 私有字段
private string m_dbconnectstring = "Data Source=;User ID=dntuser;Password=;Initial Catalog=;Pooling=true"; // 数据库连接串-格式(中文为用户修改的内容):Data Source=数据库服务器地址;User ID=您的数据库用户名;Password=您的数据库用户密码;Initial Catalog=数据库名称;Pooling=true
private string m_isencrypt = "";
private string m_gridviewdbconnectstring = "Data Source=(local);Initial Catalog=BW_ERP_Data;User ID=sa";
//private string m_tableprefix = "dnt_"; // 数据库中表的前缀
private string m_forumpath = "/"; // 论坛在站点内的路径
private string m_dbtype = "";
private int m_founderuid = 0; // 创始人
#endregion
属性#region 属性
/**//// <summary>
/// 数据库连接串
/// 格式(中文为用户修改的内容):
/// Data Source=数据库服务器地址;
/// User ID=您的数据库用户名;
/// Password=您的数据库用户密码;
/// Initial Catalog=数据库名称;Pooling=true
/// </summary>
public string Dbconnectstring
...{
get ...{ return m_dbconnectstring; }
set ...{ m_dbconnectstring = value; }
}
/**//// <summary>
/// 数据库连接串是否加密
/// 加密=yes;
/// 不加密=no;
/// </summary>
public string IsEncrypt
...{
get ...{ return m_isencrypt; }
set ...{ m_isencrypt = value; }
}
public string GridviewDbconnectstring
...{
get ...{ return m_gridviewdbconnectstring; }
set ...{ m_gridviewdbconnectstring = value; }
}
/**//// <summary>
/// 数据库中表的前缀
/// </summary>
//public string Tableprefix
//{
// get { return m_tableprefix;}
// set { m_tableprefix = value;}
//}
/**//// <summary>
/// 论坛在站点内的路径
/// </summary>
public string Forumpath
...{
get ...{ return m_forumpath; }
set ...{ m_forumpath = value; }
}
/**//// <summary>
/// 数据库类型
/// </summary>
public string Dbtype
...{
get ...{ return m_dbtype; }
set ...{ m_dbtype = value; }
}
/**//// <summary>
/// 创始人ID
/// </summary>
public int Founderuid
...{
get ...{ return m_founderuid; }
set ...{ m_founderuid = value; }
}
#endregion
}
public class BaseConfigInfo : IConfigInfo
... {
私有字段#region 私有字段
private string m_dbconnectstring = "Data Source=;User ID=dntuser;Password=;Initial Catalog=;Pooling=true"; // 数据库连接串-格式(中文为用户修改的内容):Data Source=数据库服务器地址;User ID=您的数据库用户名;Password=您的数据库用户密码;Initial Catalog=数据库名称;Pooling=true
private string m_isencrypt = "";
private string m_gridviewdbconnectstring = "Data Source=(local);Initial Catalog=BW_ERP_Data;User ID=sa";
//private string m_tableprefix = "dnt_"; // 数据库中表的前缀
private string m_forumpath = "/"; // 论坛在站点内的路径
private string m_dbtype = "";
private int m_founderuid = 0; // 创始人
#endregion
属性#region 属性
/**//// <summary>
/// 数据库连接串
/// 格式(中文为用户修改的内容):
/// Data Source=数据库服务器地址;
/// User ID=您的数据库用户名;
/// Password=您的数据库用户密码;
/// Initial Catalog=数据库名称;Pooling=true
/// </summary>
public string Dbconnectstring
...{
get ...{ return m_dbconnectstring; }
set ...{ m_dbconnectstring = value; }
}
/**//// <summary>
/// 数据库连接串是否加密
/// 加密=yes;
/// 不加密=no;
/// </summary>
public string IsEncrypt
...{
get ...{ return m_isencrypt; }
set ...{ m_isencrypt = value; }
}
public string GridviewDbconnectstring
...{
get ...{ return m_gridviewdbconnectstring; }
set ...{ m_gridviewdbconnectstring = value; }
}
/**//// <summary>
/// 数据库中表的前缀
/// </summary>
//public string Tableprefix
//{
// get { return m_tableprefix;}
// set { m_tableprefix = value;}
//}
/**//// <summary>
/// 论坛在站点内的路径
/// </summary>
public string Forumpath
...{
get ...{ return m_forumpath; }
set ...{ m_forumpath = value; }
}
/**//// <summary>
/// 数据库类型
/// </summary>
public string Dbtype
...{
get ...{ return m_dbtype; }
set ...{ m_dbtype = value; }
}
/**//// <summary>
/// 创始人ID
/// </summary>
public int Founderuid
...{
get ...{ return m_founderuid; }
set ...{ m_founderuid = value; }
}
#endregion
}
BwConf.config
<?
xml version="1.0"
?>
< BaseConfigInfo xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< Dbconnectstring > Data Source=(local);User ID=sqlname;Password=sqlpassword;Initial Catalog=databasename;Pooling=true </ Dbconnectstring >
< IsEncrypt > no </ IsEncrypt >
< GridviewDbconnectstring > Data Source=(local);Initial Catalog=BW_ERP_Data;User ID=sa;Password= </ GridviewDbconnectstring >
< Forumpath > / </ Forumpath >
< Founderuid > 0 </ Founderuid >
< Dbtype > SqlServer </ Dbtype >
</ BaseConfigInfo >
< BaseConfigInfo xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< Dbconnectstring > Data Source=(local);User ID=sqlname;Password=sqlpassword;Initial Catalog=databasename;Pooling=true </ Dbconnectstring >
< IsEncrypt > no </ IsEncrypt >
< GridviewDbconnectstring > Data Source=(local);Initial Catalog=BW_ERP_Data;User ID=sa;Password= </ GridviewDbconnectstring >
< Forumpath > / </ Forumpath >
< Founderuid > 0 </ Founderuid >
< Dbtype > SqlServer </ Dbtype >
</ BaseConfigInfo >
改自Discuz!NT