这段代码对我实在很难!请高手注释!

lijizun 2009-04-27 09:37:00
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
}
}
...全文
155 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
李世垚 2009-04-27
  • 打赏
  • 举报
回复
顶 不是很复杂 就是读配置表 通过dataset和其他程序交互
wangzhe1945 2009-04-27
  • 打赏
  • 举报
回复
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 //类DressConfig 对配置文件的操作
{
private string _fileName; //配置文件名称
private DataSet _dataSet; // DATASET
private DressConfig(string fileName) //构造函数,将配置文件地址传入
{
_fileName = fileName;
}
public string GetConfig(string key) //根据 KEY 从配置表 DATATABLE 中得到 VALUE
{
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() //从配置文件XML文件中得到一张配置表
{
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
}
}
xghtom 2009-04-27
  • 打赏
  • 举报
回复
应该算是一个简单的增删改查功能
但是访问的不是数据库,是config文件。
(查找)public string GetConfig(string key)
(新增/修改)public void SetConfig(string key, string value)
(读文件)private DataTable LoadConfig()
(创建一个DataSet/DT)private DataSet CreateDS()
其他的都有xml注释。

jie3614 2009-04-27
  • 打赏
  • 举报
回复
还是靠自己啊 都是对配置文件的读取 心要静……
llsen 2009-04-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhnzzy 的回复:]
主要功能树读写出XML文件里面内容,和DATASET做个交互
Assembly.GetExecutingAssembly()取出当前正在运行程序集,看看MSDN帮助都有
[/Quote]

up
javc 2009-04-27
  • 打赏
  • 举报
回复
似乎这段代码写的还好读啊。
public string GetConfig(string key) //通过Key 得到xml的value 的值

public void SetConfig(string key, string value) //首先通过key查询 xml数据源有没有该key 有更新;没有添加

private DataTable LoadConfig() //数据集datatable初始化

说心里话,他的这代码写的有点乱的。
可能是个范例,没有什么架构。
yanm7788 2009-04-27
  • 打赏
  • 举报
回复
up个
宝_爸 2009-04-27
  • 打赏
  • 举报
回复
看起来很大部分是读写winform程序的配置文件的代码。
zhnzzy 2009-04-27
  • 打赏
  • 举报
回复
主要功能树读写出XML文件里面内容,和DATASET做个交互
Assembly.GetExecutingAssembly()取出当前正在运行程序集,看看MSDN帮助都有
zsuswy 2009-04-27
  • 打赏
  • 举报
回复
UP,代码自己慢慢看
zhnzzy 2009-04-27
  • 打赏
  • 举报
回复
那里有问题?如果你一句都看不懂的话,那说明你基础有问题了

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧