ini文件读写
C#怎样简单读写ini文件:
1、以下是我的ini文件Config.ini的格式。
[ScanPath]
Version=8
Version=8
2、代码实现:
1
[DllImport("kernel32")]//返回0表示失败,非0为成功
2
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
[DllImport("kernel32")]//返回取得字符串缓冲区的长度
5
private static extern long GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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>
15
public 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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>
47
public 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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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