C#怎样简单读写ini文件:
1、以下是我的ini文件Config.ini的格式。
[ScanPath]
Version=8
Version=8
2、代码实现:
1[DllImport("kernel32")]//返回0表示失败,非0为成功
2private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
3
4[DllImport("kernel32")]//返回取得字符串缓冲区的长度
5private static extern long GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
6
7
8/// <summary>写Ini文件
9/// </summary>
10/// <param name="Section">开始节点</param>
11/// <param name="Key">Key名</param>
12/// <param name="Value">Key的值</param>
13/// <param name="iniFilePath">init的文件路径</param>
14/// <returns>是否修改成功</returns>
15public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
16{
17 if (!File.Exists(iniFilePath))
18 {
19 FileStream stream = File.Create(iniFilePath);
20 stream.Close();
21 }
22
23 if (File.Exists(iniFilePath))
24 {
25 long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
26 if (OpStation == 0)
27 {
28 return false;
29 }
30 else
31 {
32 return true;
33 }
34 }
35 else
36 {
37 return false;
38 }
39}
40
41/// <summary>读Ini文件
42/// </summary>
43/// <param name="Section">父节点</param>
44/// <param name="Key">Key节点</param>
45/// <param name="iniFilePath">init的文件路径</param>
46/// <returns>返回读取到的值</returns>
47public static string ReadIniData(string Section,string Key,string iniFilePath)
48{
49 if(File.Exists(iniFilePath))
50 {
51 StringBuilder temp = new StringBuilder(1024);
52 GetPrivateProfileString(Section,Key,string.Empty,temp,1024,iniFilePath);
53 return temp.ToString();
54 }
55 else
56 {
57 return String.Empty;
58 }
59}
60
2private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
3
4[DllImport("kernel32")]//返回取得字符串缓冲区的长度
5private static extern long GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
6
7
8/// <summary>写Ini文件
9/// </summary>
10/// <param name="Section">开始节点</param>
11/// <param name="Key">Key名</param>
12/// <param name="Value">Key的值</param>
13/// <param name="iniFilePath">init的文件路径</param>
14/// <returns>是否修改成功</returns>
15public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
16{
17 if (!File.Exists(iniFilePath))
18 {
19 FileStream stream = File.Create(iniFilePath);
20 stream.Close();
21 }
22
23 if (File.Exists(iniFilePath))
24 {
25 long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
26 if (OpStation == 0)
27 {
28 return false;
29 }
30 else
31 {
32 return true;
33 }
34 }
35 else
36 {
37 return false;
38 }
39}
40
41/// <summary>读Ini文件
42/// </summary>
43/// <param name="Section">父节点</param>
44/// <param name="Key">Key节点</param>
45/// <param name="iniFilePath">init的文件路径</param>
46/// <returns>返回读取到的值</returns>
47public static string ReadIniData(string Section,string Key,string iniFilePath)
48{
49 if(File.Exists(iniFilePath))
50 {
51 StringBuilder temp = new StringBuilder(1024);
52 GetPrivateProfileString(Section,Key,string.Empty,temp,1024,iniFilePath);
53 return temp.ToString();
54 }
55 else
56 {
57 return String.Empty;
58 }
59}
60
3、调用:
string ReadValue = IniFile.ReadIniData("ScanPath", "Version", "Config.ini");
输出结果:
8
IniFile.WriteIniData("ScanPath", "Version", "25", "Config.ini");
string ReadValue = IniFile.ReadIniData("ScanPath", "Version", "Config.ini");
输出结果:
25
输出结果:
8
IniFile.WriteIniData("ScanPath", "Version", "25", "Config.ini");
string ReadValue = IniFile.ReadIniData("ScanPath", "Version", "Config.ini");
输出结果:
25