这段代码对我实在很难!请高手注释!
using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web.Services.Protocols;
namespace Dress
{
class DressConfig
{
private string _fileName;
private DataSet _dataSet;
private DressConfig(string fileName)
{
_fileName = fileName;
}
public string GetConfig(string key)
{
DataTable dt = this.LoadConfig();
foreach (DataRow row in dt.Rows)
{
if (row["key"].ToString().ToLower() == key.ToLower())
return row["value"].ToString().Trim();
}
return null;
}
public void SetConfig(string key, string value)
{
DataTable dt = this.LoadConfig();
foreach (DataRow row in dt.Rows)
{
if (row["key"].ToString().ToLower() == key.ToLower())
{
row["value"] = value;
return;
}
}
DataRow nRow = dt.NewRow();
nRow["key"] = key;
nRow["value"] = value;
dt.Rows.Add(nRow);
}
private DataTable LoadConfig()
{
if (_dataSet == null)
{
if (File.Exists(_fileName))
{
_dataSet = new DataSet();
_dataSet.ReadXml(_fileName, XmlReadMode.ReadSchema);
}
else
{
_dataSet = this.CreateDS();
}
}
return _dataSet.Tables[0];
}
public void SaveConfig()
{
_dataSet.WriteXml(_fileName, XmlWriteMode.WriteSchema);
}
private DataSet CreateDS()
{
DataTable dt = new DataTable();
dt.Columns.Add("key", typeof(string));
dt.Columns.Add("value", typeof(string));
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
#region static memeber...
static DressConfig m_Config;
static DressConfig()
{
string fileName = Assembly.GetExecutingAssembly().GetName().CodeBase + ".config";
m_Config = new DressConfig(fileName);
}
public static void SetValue(string key,string value)
{
m_Config.SetConfig(key, value);
}
public static string GetValue(string key)
{
return m_Config.GetConfig(key);
}
/// <summary>
/// 保存配置文件。
/// </summary>
public static void Save()
{
m_Config.SaveConfig();
}
private static string _serviceUrl;
/// <summary>
/// 获取或设置Web Service连接串。
/// </summary>
public static string ServiceUrl
{
get
{
if (_serviceUrl == null)
{
string url = DressConfig.GetValue("ServiceUrl");
if (string.IsNullOrEmpty(url))
_serviceUrl = "http://localhost/DressSrv";
else
_serviceUrl = url;
}
return _serviceUrl;
}
set
{
_serviceUrl = value;
DressConfig.SetValue("ServiceUrl", value); //SetValue 设定值 ; DressConfig //穿着配置
}
}
/// <summary>
/// 配置Web Service 代理实例。
/// </summary>
/// <param name="instance"></param>
public static void ConfigProxyUrl(SoapHttpClientProtocol instance) //instance 例如
{
string asmx = instance.Url.Remove(0, instance.Url.LastIndexOf('/') + 1);
instance.Url = DressConfig.ServiceUrl + "/" + asmx;
}
private static string _appRoot;
/// <summary>
/// 应用程序运行目录。
/// </summary>
public static string AppRoot //AppRoot 应用程序根
{
get
{
if (_appRoot == null)
{
string asmFile = Assembly.GetExecutingAssembly().GetName().CodeBase; //Assembly 大会
_appRoot = asmFile.Substring(0, asmFile.LastIndexOf('\\'));
}
return _appRoot;
}
}
#endregion
}
}