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)
{
List.Add(new ConfigManagerKeyValue((string)Row["key"],Row["value"]));
}
}
catch{}
return List;
}
/// <summary>
/// 增加一个值
/// </summary>
/// <param name="KeyName">键</param>
/// <param name="Value">值</param>
/// <returns>成功:true;失败:false</returns>
public bool AddValue(string KeyName,object Value)
{
DataRowView Row;
try
{
dvDataView.RowFilter="key='" + KeyName + "'";
if(dvDataView.Count==0)
{
Row = dvDataView.AddNew();
Row["key"] = KeyName;
Row["value"] = Value;
Row.EndEdit();
saveData();
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}
/// <summary>
/// 删除一个值
/// </summary>
/// <param name="KeyName">键</param>
/// <returns>成功:true;失败:false</returns>
public bool RemoveValue(string KeyName)
{
DataRowView Row;
dvDataView.RowFilter="key='" + KeyName + "'";
if(dvDataView.Count != 0)
{
Row = dvDataView[0];
Row.Delete();
return true;
}
else
{
return false;
}
}
/// <summary>
/// 更新值
/// </summary>
/// <param name="KeyName">键</param>
/// <param name="Value">值</param>
/// <returns>成功:true;失败:false</returns>
public bool UpdateValue(string KeyName,object Value)
{
RemoveValue(KeyName);
return AddValue(KeyName,Value);
}
private void saveData()
{
dsDataSet.WriteXml(pstrFilename);
}
private DataTable createDataTable()
{
DataTable tbl = new DataTable();
tbl.TableName = "Configuration";
tbl.Columns.Add("key",typeof(string));
tbl.Columns.Add("value",typeof(string));
return(tbl);
}
/// <summary>
/// 文件名
/// </summary>
public string Filename
{
get{return pstrFilename;}
}
}
}