You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
6.4 KiB
C#
181 lines
6.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Web.Caching;
|
|
using System.Xml.Serialization;
|
|
using System.Xml;
|
|
using System.Configuration;
|
|
|
|
namespace CommonFunc
|
|
{
|
|
/// <summary>
|
|
/// DatabaseConfig 的摘要说明。
|
|
/// </summary>
|
|
public class DatabaseConfig
|
|
{
|
|
public DatabaseConfig()
|
|
{
|
|
//
|
|
// TODO: 在此处添加构造函数逻辑
|
|
//
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取配置信息
|
|
/// </summary>
|
|
/// <returns>ConnectionConfig</returns>
|
|
public static ConnectionConfig GetSettings()
|
|
{
|
|
return GetSettings("config");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取配置信息
|
|
/// </summary>
|
|
/// <returns>ConnectionConfig</returns>
|
|
public static ConnectionConfig GetSettingsDirectory()
|
|
{
|
|
return GetSettingsDirectory("config");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取配置信息
|
|
/// </summary>
|
|
/// <param name="subName">物理路径</param>
|
|
/// <returns></returns>
|
|
public static ConnectionConfig GetSettingsDirectory(string subName)
|
|
{
|
|
ConnectionConfig data = new ConnectionConfig();
|
|
try
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(ConnectionConfig));
|
|
FileStream fs = new FileStream(subName + "\\ConnectionConfig.Config", FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
data = (ConnectionConfig)serializer.Deserialize(fs);
|
|
fs.Close();
|
|
//HttpContext context = HttpContext.Current;
|
|
|
|
//data = (ConnectionConfig)context.Cache["ConnectionConfig"];
|
|
|
|
//if (data == null)
|
|
//{
|
|
// XmlSerializer serializer = new XmlSerializer(typeof(ConnectionConfig));
|
|
// try
|
|
// {
|
|
// string fileName = context.Server.MapPath(subName);
|
|
// if (fileName.Substring(fileName.Length - 1, 1) != "\\")
|
|
// fileName += "\\";
|
|
// fileName += "ConnectionConfig.Config";
|
|
|
|
// int i = 0;
|
|
// while (!System.IO.File.Exists(fileName) && i <= 10)
|
|
// {
|
|
// subName = "../" + subName;
|
|
// fileName = context.Server.MapPath(subName);
|
|
// if (fileName.Substring(fileName.Length - 1, 1) != "\\")
|
|
// fileName += "\\";
|
|
// fileName += "ConnectionConfig.Config";
|
|
// i++;
|
|
// }
|
|
|
|
// FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
// data = (ConnectionConfig)serializer.Deserialize(fs);
|
|
// fs.Close();
|
|
// context.Cache.Insert("ConnectionConfig", data, new CacheDependency(fileName));
|
|
// }
|
|
// catch (System.IO.FileNotFoundException)
|
|
// {
|
|
// data = new ConnectionConfig();
|
|
// }
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(ConnectionConfig));
|
|
FileStream fs = new FileStream(subName+ "\\ConnectionConfig.Config", FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
data = (ConnectionConfig)serializer.Deserialize(fs);
|
|
fs.Close();
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取配置信息
|
|
/// </summary>
|
|
/// <param name="relativePath">虚拟路径</param>
|
|
/// <returns></returns>
|
|
public static ConnectionConfig GetSettings(string relativePath)
|
|
{
|
|
HttpContext context = HttpContext.Current;
|
|
|
|
ConnectionConfig data = (ConnectionConfig)context.Cache["ConnectionConfig"];
|
|
|
|
if (data == null)
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(ConnectionConfig));
|
|
try
|
|
{
|
|
string fileName = context.Server.MapPath(relativePath + "/ConnectionConfig.Config");
|
|
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
data = (ConnectionConfig)serializer.Deserialize(fs);
|
|
fs.Close();
|
|
context.Cache.Insert("ConnectionConfig", data, new CacheDependency(fileName));
|
|
}
|
|
catch (System.IO.FileNotFoundException)
|
|
{
|
|
|
|
data = new ConnectionConfig();
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新Cache的内容
|
|
/// </summary>
|
|
public static void UpdateCache()
|
|
{
|
|
UpdateCache("config");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新Cache的内容
|
|
/// </summary>
|
|
/// <param name="subName">物理路径目录</param>
|
|
public static void UpdateCache(string subName)
|
|
{
|
|
HttpContext context = HttpContext.Current;
|
|
XmlSerializer serializer = new XmlSerializer(typeof(ConnectionConfig));
|
|
try
|
|
{
|
|
string fileName = context.Server.MapPath(subName);
|
|
if (fileName.Substring(fileName.Length - 1, 1) != "\\")
|
|
fileName += "\\";
|
|
fileName += "ConnectionConfig.Config";
|
|
|
|
int i = 0;
|
|
while (!System.IO.File.Exists(fileName) && i <= 10)
|
|
{
|
|
subName = "../" + subName;
|
|
fileName = context.Server.MapPath(subName);
|
|
if (fileName.Substring(fileName.Length - 1, 1) != "\\")
|
|
fileName += "\\";
|
|
fileName += "ConnectionConfig.Config";
|
|
i++;
|
|
}
|
|
|
|
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
ConnectionConfig data = (ConnectionConfig)serializer.Deserialize(fs);
|
|
fs.Close();
|
|
context.Cache.Insert("ConnectionConfig", data, new CacheDependency(fileName));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|