本文实例为大家分享了C#语言实现ini文件读写操作的具体代码,供大家参考,具体内容如下
1、ini文件是什么?
见百度百科
2、C#语言实现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
|
/// <summary>
/// 配置文件 .ini操作类
/// </summary>
public class IniFileUtils
{
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="section">节点名称[如TypeName]</param>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
[DllImport( "kernel32" )]
private static extern long WritePrivateProfileString( string section, string key, string val, string filepath);
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">节点名称</param>
/// <param name="key">键</param>
/// <param name="def">值</param>
/// <param name="retval">stringbulider对象</param>
/// <param name="size">字节大小</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
[DllImport( "kernel32" )]
private static extern int GetPrivateProfileString( string section, string key, string def, StringBuilder retval, int size, string filePath);
/// <summary>
/// 写入或更新.ini配置文件属性值
/// </summary>
/// <param name="section">区域(节点)</param>
/// <param name="key">key键属性名称</param>
/// <param name="value">key键对应属性值param>
/// <param name="path">.ini文件所在路径</param>
public static void WriteContentValue( string section, string key, string value, string path)
{
//判断文件是或否存在
if (File.Exists(path))
{
WritePrivateProfileString(section, key, value, path);
}
}
/// <summary>
/// 读取.ini配置文件属性值
/// </summary>
/// <param name="Section">区域(节点)</param>
/// <param name="key">key键属性名称</param>
/// <param name="path">.ini文件所在路径</param>
/// <returns></returns>
public static string ReadContentValue( string Section, string key, string path)
{
StringBuilder temp = new StringBuilder(1024);
//判断文件是或否存在
if (File.Exists(path))
{
GetPrivateProfileString(Section, key, "" , temp, 1024, path);
}
return temp.ToString();
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/obchy/p/9429400.html