日期:2009-03-18  浏览次数:20544 次

using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

using System.Collections;

namespace DataLibrary
{
/// <summary>
/// 配置底层
/// </summary>
public class ConfigManagerKeyValue
{
private string pstrKey;
private object pobjValue;
/// <summary>
/// 配置管理键值
/// </summary>
/// <param name="Key"></param>
/// <param name="Value"></param>
public ConfigManagerKeyValue(string Key,object Value)
{
pstrKey = Key;
pobjValue = Value;
}

/// <summary>
/// 键
/// </summary>
public string Key
{
get{return pstrKey;}
set{pstrKey=value;}
}

/// <summary>
/// 值
/// </summary>
public object Value
{
get{return pobjValue;}
set{pobjValue=value;}
}
}

/// <summary>
/// 配置管理业务
/// </summary>
public class DisposalConductService
{
private string pstrFilename;
private DataSet dsDataSet;
private DataView dvDataView;
/// <summary>
/// 配置管理业务
/// </summary>
/// <param name="Filename"></param>
public DisposalConductService(string Filename)
{
pstrFilename = Filename;
initObjects();
}

/// <summary>
/// 配置管理业务
/// </summary>
public DisposalConductService()
{
pstrFilename = System.Reflection.Assembly.GetExecutingAssembly().ToString() + ".exe.config";
AppDomain Ad = AppDomain.CurrentDomain;
pstrFilename = Ad.BaseDirectory + pstrFilename;
initObjects();
}

private void initObjects()
{
initDataSource();
}

private void initDataSource()
{
dsDataSet = new DataSet();
try
{
dsDataSet.ReadXML(pstrFilename);
dvDataView = dsDataSet.Tables[0].DefaultView;
dvDataView.AllowEdit = true;
dvDataView.AllowDelete = true;
dvDataView.AllowNew = true;
}
catch(Exception)
{
dsDataSet.Tables.Add(createDataTable() );
dsDataSet.DataSetName = "Application";
try
{
saveData();
dvDataView = dsDataSet.Tables[0].DefaultView;
dvDataView.AllowEdit = true;
dvDataView.AllowDelete = true;
dvDataView.AllowNew = true;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}

/// <summary>
/// 取得数据值
/// </summary>
/// <param name="KeyName">键名</param>
/// <param name="Default">默认值</param>
/// <returns>值</returns>
public object GetValue(string KeyName,object Default)
{
object strResult=Default;
DataRowView Row;
dvDataView.RowFilter="key='" + KeyName + "'";
if(dvDataView.Count != 0)
{
Row = dvDataView[0];
strResult = Row["value"];
}
else
{
AddValue(KeyName,Default);
}
return(strResult);
}

/// <summary>
/// 取得值
/// </summary>
/// <returns>值</returns>
public ArrayList GetValues()
{
ArrayList List = new ArrayList();
try
{
dvDataView.RowFilter="";
foreach(DataRowView Row in dvDataView)
{