0%

csharp XML

XML创建、读取配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
static public void WirteConfig(string FileName, string KeyName, string Value)
{
try
{
string FilePath = OnlyOneStartUp.UseConfigPath;
string AllFileName = FilePath + "\\" + FileName + ".xml";
if (!Directory.Exists(FilePath))
Directory.CreateDirectory(FilePath);

DataSet ds = new DataSet();
if (File.Exists(AllFileName))
ds.ReadXml(AllFileName);
if (ds.Tables.Count < 1)
ds.Tables.Add();
if (ds.Tables[0].Rows.Count < 1)
ds.Tables[0].Rows.Add();

if (!ds.Tables[0].Columns.Contains(KeyName))
ds.Tables[0].Columns.Add(KeyName);
ds.Tables[0].Rows[0][KeyName] = Value;

ds.WriteXml(AllFileName);
}
catch (Exception e)
{
string sError = string.Format("写入配置信息Error:{0}", e.Message);
ErrorOut(MethodInfo.GetCurrentMethod().Name, sError);
}
}
/// <summary>
/// 读出配置文件
/// </summary>
/// <param name="FileName">配置文件名称</param>
/// <param name="KeyName">键名</param>
/// <param name="Value">返回的值</param>
static public bool ReadConfig(string FileName, string KeyName, ref string Value)
{
try
{
string FilePath = OnlyOneStartUp.UseConfigPath;
string AllFileName = FilePath + "\\" + FileName + ".xml";
if (!Directory.Exists(FilePath))
return false;

DataSet ds = new DataSet();
if (File.Exists(AllFileName))
ds.ReadXml(AllFileName);
else return false;
if (ds.Tables.Count < 1)
return false;
if (ds.Tables[0].Rows.Count < 1)
return false;

if (!ds.Tables[0].Columns.Contains(KeyName))
return false;
Value = Convert.ToString(ds.Tables[0].Rows[0][KeyName]);
return true;
}
catch (Exception e)
{
string sError = string.Format("读取配置信息Error:{0}", e.Message);
ErrorOut(MethodInfo.GetCurrentMethod().Name, sError);
return false;
}
}

static public void ReadConfigEx(string FileName, string KeyName, ref string Value)
{
if (!ReadConfig(FileName, KeyName, ref Value))
WirteConfig(FileName, KeyName, Value);
}