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![None.gif](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![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <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![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
17
if (!File.Exists(iniFilePath))
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
FileStream stream = File.Create(iniFilePath);
20
stream.Close();
21
}
22![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
if (File.Exists(iniFilePath))
24![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
26
if (OpStation == 0)
27![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
return false;
29
}
30
else
31![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return true;
33
}
34
}
35
else
36![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
return false;
38
}
39
}
40![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <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![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
49
if(File.Exists(iniFilePath))
50![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
StringBuilder temp = new StringBuilder(1024);
52
GetPrivateProfileString(Section,Key,string.Empty,temp,1024,iniFilePath);
53
return temp.ToString();
54
}
55
else
56![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
return String.Empty;
58
}
59
}
60
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
17
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
19
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
25
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
28
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
32
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
37
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
40
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
42
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
47
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
49
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
51
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
57
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
60
![None.gif](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