IniController


同上文一樣,在寫案子上使用過的一個 Ini 的控制類..
不錯用的,我將他改了編碼,現在應該可以支援 中文 名稱?
使用時記得改回 namespace 的名稱…

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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace SKFWCodeView
{
class IniController
{

#region vars, private functions and api calls
//
private string _iniFilename = null;
private int _bufferLen = 512;

//
[DllImport("kernel32", SetLastError = true)]
private static extern int WritePrivateProfileString(string pSection, string pKey, string pValue, string pFile);

[DllImport("kernel32", SetLastError = true)]
private static extern int WritePrivateProfileStruct(string pSection, string pKey, string pValue, int pValueLen, string pFile);

[DllImport("kernel32", SetLastError = true)]
private static extern int GetPrivateProfileString(string pSection, string pKey, string pDefault, byte[] prReturn, int pBufferLen, string pFile);

[DllImport("kernel32", SetLastError = true)]
private static extern int GetPrivateProfileStruct(string pSection, string pKey, byte[] prReturn, int pBufferLen, string pFile);

//
private string getString(string section, string key, string defaultValue)
{
string sRet = defaultValue;
byte[] bRet = new byte[_bufferLen];
int i = GetPrivateProfileString(section, key, defaultValue, bRet, _bufferLen, _iniFilename);
sRet = System.Text.Encoding.GetEncoding(950).GetString(bRet, 0, i).TrimEnd((char)0);
return sRet;
}
#endregion

//
public IniController(string iniFilename)
{
this._iniFilename = iniFilename;
}

//
public string IniFile
{
get { return this._iniFilename; }
set { this._iniFilename = value; }
}

//
public int BufferLen
{
get { return this._bufferLen; }
set
{
if (value > 32767) { this._bufferLen = 32767; }
else if (value < 1) { this._bufferLen = 1; }
else { this._bufferLen = value; }
}
}

//
public string ReadValue(string section, string key, string defaultValue)
{
return getString(section, key, defaultValue);
}

//
public string ReadValue(string section, string key)
{
return getString(section, key, "");
}

//
public void WriteValue(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, this._iniFilename);
}

//
public void RemoveValue(string section, string key)
{
WritePrivateProfileString(section, key, null, this._iniFilename);
}

//
public string[] ReadKeys(string section)
{
return getString(section, null, null).Split((char)0);
}

//
public string[] ReadSections()
{
return getString(null, null, null).Split((char)0);
}

//
public void RemoveSection(string section)
{
WritePrivateProfileString(section, null, null, this._iniFilename);
}

}
}