0%

CSharp读写Ini文件

自用

代码

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
public class IniFile
{
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(byte[] section, byte[] key, byte[] val, string filePath);

[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetPrivateProfileString(byte[] section, byte[] key, byte[] def, System.Text.StringBuilder retVal, int size, string filePath);

[System.Runtime.InteropServices.DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern uint GetPrivateProfileString2(byte[] section, byte[] key, byte[] def, byte[] retVal, int size, string filePath);

private string sPath = null;

public IniFile(string path)
{
this.sPath = path;
}

// section=配置节,key=键名,value=键值
public void WriteIni(string section, string key, string value)
{
WritePrivateProfileString(GetBytes(section), GetBytes(key), GetBytes(value), sPath);
}

public void WriteIni(string section, string key, string value, string sPath)
{
WritePrivateProfileString(GetBytes(section), GetBytes(key), GetBytes(value), sPath);
}

// section=配置节,key=键名
public string ReadIni(string section, string key, string sPath)
{
System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
GetPrivateProfileString(GetBytes(section), GetBytes(key), GetBytes(""), temp, 255, sPath);
//return temp.ToString();
return Encoding.UTF8.GetString(Encoding.Default.GetBytes(temp.ToString()));
}

public string ReadIni(string section, string key)
{
System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
GetPrivateProfileString(GetBytes(section), GetBytes(key), GetBytes(""), temp, 255, sPath);
//return temp.ToString();
return Encoding.UTF8.GetString(Encoding.Default.GetBytes(temp.ToString()));
}

public void GreatIni(string path)
{
if (!File.Exists(path))
{
FileStream FS = new FileStream(path, FileMode.CreateNew);
FS.Close();
}
}

private byte[] GetBytes(string str, string encodingName = "utf-8")
{
return str == null ? null : Encoding.GetEncoding(encodingName).GetBytes(str);
}

public List<string> ReadSections(string iniFilename)
{
List<string> result = new List<string>();
Byte[] buf = new Byte[65536];
uint len = GetPrivateProfileString2(null, null, null, buf, buf.Length, iniFilename);
int j = 0;
for (int i = 0; i < len; i++)
if (buf[i] == 0)
{
result.Add(Encoding.UTF8.GetString(buf, j, i - j));
j = i + 1;
}
return result;
}

public List<string> ReadKeys(string SectionName)
{
List<string> result = new List<string>();
Byte[] buf = new Byte[65536];
uint len = GetPrivateProfileString2(GetBytes(SectionName), null, null, buf, buf.Length, sPath);
int j = 0;
for (int i = 0; i < len; i++)
if (buf[i] == 0)
{
result.Add(Encoding.UTF8.GetString(buf, j, i - j));
j = i + 1;
}
return result;
}

public Dictionary<string, string> ReadKeyValues(string SectionName)
{
Dictionary<string, string> keyValues = new Dictionary<string, string>();
List<string> keys = ReadKeys(SectionName);
keys.ForEach((key) =>
{
keyValues.Add(key, ReadIni(SectionName, key));
});
return keyValues;
}
}