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
class Ini
{
[System.Runtime.InteropServices.DllImport("kernel32", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
/// <summary>
///
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="def">没有找到对应值时返回的值</param>
/// <param name="retVal">String.Builder获取读取到的值</param>
/// <param name="size"></param>
/// <param name="filePath"></param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("kernel32", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

string ConfPath;
public Ini(string path)
{
ConfPath = path;
}
public bool Write(string section, string key, string value)
{
if (WritePrivateProfileString(section, key, value, ConfPath) != 0)
{
return true;
}
else
{
return false;
}
}
public string Read(string section,string key)
{
StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString(section, key, "", temp, 255, ConfPath);
return temp.ToString();
}
}