高效處理 INI 文件 (WM 用)


在網路上發現的代碼..不過還沒測試..先留著吧
相信不久將來 Window Mobile 的程式會用上

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class IniCF
{
/// <summary>
/// Copyright (C) 2008 [email protected]
/// Ini文件訪問類(基於Compact Framework 2.0)
/// 說明:該類是在Window Mobile上使用的,所以為了保證執行高效,丟失了一些ini文件的特性。比如文件中不能有空行,否則讀取不到。

// Fields
public string path = string.Empty;

// Methods
public IniCF(string iniFilePath)
{
this.path = iniFilePath;
}
#region 讀取
/// <summary>
/// 讀取
/// </summary>
/// <param name="ApplicationName">節名</param>
/// <param name="KeyName">鍵名</param>
/// <param name="Default">默認值</param>
/// <param name="FileName">ini文件名</param>
/// <returns></returns>
private string GetPrivateProfileString(string ApplicationName, string KeyName, string Default, string FileName)
{
string str = string.Format("[{0}]", ApplicationName);
string str2 = KeyName + "=";
StreamReader reader = null;
try
{
string str3 = string.Empty;
bool flag = false;
reader = new StreamReader(FileName, Encoding.Default);
str3 = reader.ReadLine();
for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
{
if (!flag)
{
if (str3 == str)
{
flag = true;
}
}
else
{
if (str3.StartsWith("[") && str3.EndsWith("]"))
{
return Default;
}
if (str3.StartsWith(str2))
{
return str3.Substring(KeyName.Length + 1);
}
}
str3 = reader.ReadLine();
}
}
catch
{
}
finally
{
if (reader != null)
{
reader.Close();
}
}
return Default;
}
#endregion

#region 寫入

/// <summary>
/// ini文件寫入
/// </summary>
/// <param name="ApplicationName">節名</param>
/// <param name="KeyName">鍵名</param>
/// <param name="Value">要寫入的值</param>
/// <param name="FileName">ini文件</param>
private void WritePrivateProfileString(string ApplicationName, string KeyName, string Value, string FileName)
{
string str = string.Format("[{0}]", ApplicationName);
string str2 = KeyName + "=";
try
{
StringBuilder builder = new StringBuilder();
bool flag = false;
bool flag2 = false;
bool flag3 = true;
StreamReader reader = new StreamReader(FileName, Encoding.Default);
string str3 = reader.ReadLine();
for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
{
if (flag3)
{
if (!flag2)
{
builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
builder.Append(str3);
if (str3 == str)
{
flag2 = true;
}
}
else if (str3.StartsWith("[") && str3.EndsWith("]"))
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
builder.Append(string.Format("\r\n{0}", str3));
flag = true;
flag3 = false;
}
else if (str3.StartsWith(str2))
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
flag = true;
flag3 = false;
}
else
{
builder.Append(string.Format("\r\n{0}", str3));
}
}
else
{
builder.Append(string.Format("\r\n{0}", str3));
}
str3 = reader.ReadLine();
}
reader.Close();
if (!flag2)
{
builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
builder.Append(string.Format("[{0}]", ApplicationName));
}
if (!flag)
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
}
StreamWriter writer = new StreamWriter(FileName, false, Encoding.Default);
writer.Write(builder);
writer.Close();
}
catch
{
}
}
#endregion

public string IniReadValue(string Section, string Key)
{
return this.GetPrivateProfileString(Section, Key, string.Empty, this.path);
}

public void IniWriteValue(string Section, string Key, string Value)
{
this.WritePrivateProfileString(Section, Key, Value, this.path);
}
}