用C#读写ini配置文件完整范例
来源:互联网 发布:mysql 记录创建时间 编辑:程序博客网 时间:2024/05/02 04:26
【使用说明】
INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数.
INI文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键字(KEYWORD)和一个等号,等号右边就是关键字的值(VALUE).例如:
[Section1]
KeyWord1 = Value1
KeyWord2 = Value2
...
[Section2]
KeyWord3 = Value3
KeyWord4 = Value4
C#命名空间中没有直接读写INI的类,当然如果你把INT当成文本文件用System.IO类来读写算我没说.
我现在介绍的是系统处理INI的方法.
虽然C#中没有,但是在"kernel32.dll"这个文件中有Win32的API函数--WritePrivateProfileString()和GetPrivateProfileString()
C#声明INI文件的写操作函数WritePrivateProfileString():
1
[DllImport(
"kernel32"
)]
2
private
static
extern
long
WritePrivateProfileString(
string
section,
string
key,
string
val,
string
filePath);
C#申明INI文件的读操作函数GetPrivateProfileString():
1
[DllImport(
"kernel32"
)]
2
private
static
extern
int
GetPrivateProfileString(
string
section,
string
key,
string
def, StringBuilder retVal,
int
size,
string
filePath);
【调试截图】
图一、
图二、
图三、
【代码展示(IniClass.cs)】
01
using
System;
02
using
System.Collections.Generic;
03
using
System.Web;
04
using
System.Runtime.InteropServices;
05
using
System.Text;
06
using
System.IO;
07
08
namespace
IniFileOperation.Web
09
{
10
public
class
IniClass
11
{
12
public
string
inipath;
13
[DllImport(
"kernel32"
)]
14
private
static
extern
long
WritePrivateProfileString(
string
section,
string
key,
string
val,
string
filePath);
15
[DllImport(
"kernel32"
)]
16
private
static
extern
int
GetPrivateProfileString(
string
section,
string
key,
string
def, StringBuilder retVal,
int
size,
string
filePath);
17
18
/// <summary>
19
/// 构造方法
20
/// </summary>
21
/// <param name="IniPath">文件路径</param>
22
public
IniClass(
string
IniPath)
23
{
24
inipath = IniPath;
25
}
26
27
28
/// <summary>
29
/// 写入Ini文件
30
/// </summary>
31
/// <param name="Section">项目名称(如 [TypeName] )</param>
32
/// <param name="Key">键</param>
33
/// <param name="Value">值</param>
34
/// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符。</returns>
35
public
long
IniWriteValue(
string
Section,
string
Key,
string
Value)
36
{
37
return
WritePrivateProfileString(Section, Key, Value,
this
.inipath);
38
}
39
40
/// <summary>
41
/// 读出INI文件
42
/// </summary>
43
/// <param name="Section">项目名称(如 [TypeName] )</param>
44
/// <param name="Key">键</param>
45
public
string
IniReadValue(
string
Section,
string
Key)
46
{
47
StringBuilder temp =
new
StringBuilder(500);
48
int
i = GetPrivateProfileString(Section, Key,
""
, temp, 500,
this
.inipath);
49
return
temp.ToString();
50
}
51
52
/// <summary>
53
/// 验证文件是否存在
54
/// </summary>
55
/// <returns>布尔值</returns>
56
public
bool
ExistIniFile()
57
{
58
return
File.Exists(inipath);
59
}
60
}
61
}
01
using
System;
02
using
System.Collections.Generic;
03
using
System.Web;
04
using
System.Web.UI;
05
using
System.Web.UI.WebControls;
06
07
namespace
IniFileOperation.Web
08
{
09
public
partial
class
_Default : System.Web.UI.Page
10
{
11
protected
void
Page_Load(
object
sender, EventArgs e)
12
{
13
14
}
15
16
/// <summary>
17
/// 判断ini文件是否存在按钮单击事件
18
/// </summary>
19
/// <param name="sender"></param>
20
/// <param name="e"></param>
21
protected
void
Button1_Click(
object
sender, EventArgs e)
22
{
23
//获取用户输入的INI文件的路径
24
string
iniPath = @TextBox1.Text.ToString().Trim();
25
//初始化构造函数和对象
26
IniClass iniClass =
new
IniClass(iniPath);
27
//判断ini文件是否存在
28
if
(iniClass.ExistIniFile())
29
{
30
Response.Write(
"ini文件存在!路径是:"
+ iniPath);
31
}
32
else
33
{
34
Response.Write(
"路径为:"
+ iniPath +
"处不存在该配置文件。"
);
35
}
36
//销毁对象
37
iniClass =
null
;
38
}
39
40
/// <summary>
41
/// 写入ini文件按钮单击事件
42
/// </summary>
43
/// <param name="sender"></param>
44
/// <param name="e"></param>
45
protected
void
Button2_Click(
object
sender, EventArgs e)
46
{
47
//获取用户输入的INI文件的路径
48
string
iniPath = @TextBox1.Text.ToString().Trim();
49
//初始化构造函数和对象
50
IniClass iniClass =
new
IniClass(iniPath);
51
Response.Write(
"ini文件第一次的写入字节数量为:"
+ iniClass.IniWriteValue(
"SYSDNSection"
,
"SYSDNKey1"
,
"SYSDNValue1"
) +
"<br/>"
);
52
Response.Write(
"ini文件第二次的写入字节数量为:"
+ iniClass.IniWriteValue(
"SYSDNSection"
,
"SYSDNKey2"
,
"SYSDNValue2"
) +
"<br/>"
);
53
//销毁对象
54
iniClass =
null
;
55
}
56
57
/// <summary>
58
/// 读取ini文件按钮单击事件
59
/// </summary>
60
/// <param name="sender"></param>
61
/// <param name="e"></param>
62
protected
void
Button3_Click(
object
sender, EventArgs e)
63
{
64
//获取用户输入的INI文件的路径
65
string
iniPath = @TextBox1.Text.ToString().Trim();
66
//初始化构造函数和对象
67
IniClass iniClass =
new
IniClass(iniPath);
68
//读取配置文件Key1和Key2的值,并显示到label里
69
Label1.Text = iniClass.IniReadValue(
"SYSDNSection"
,
"SYSDNKey1"
)+
"<br/>"
;
70
Label1.Text += iniClass.IniReadValue(
"SYSDNSection"
,
"SYSDNKey2"
);
71
//销毁对象
72
iniClass =
null
;
73
}
74
}
75
}
- 用C#读写ini配置文件完整范例
- 用C#读写ini配置文件
- 用C#读写ini配置文件
- 用C#读写ini配置文件
- 用C#读写ini配置文件
- 用C#读写ini配置文件
- C#读写INI配置文件
- C#读写ini配置文件
- C#读写ini配置文件
- C#读写ini配置文件
- C#读写INI配置文件
- C#读写ini配置文件
- C#中读写INI配置文件
- C#中读写INI配置文件
- C#中读写INI配置文件
- c# 读写INI配置文件 张宇轩
- C# API读写ini配置文件
- 使用C#读写ini配置文件
- 国外程序员推荐的免费编程书籍资源
- Service使用技巧,一些不为人知的功能 [移动平台 Android]
- C语言中系统函数在linux下的使用
- How tnsping work: Comparison of Oracle's tnsping to TCP/IP's ping [ID 146264.1]
- MFC文档应用程序实现文件拖拽到应用程序中显示
- 用C#读写ini配置文件完整范例
- Android控件组合应用四
- hdu 1058 hdoj 1058
- 在Visual C++中用ADO进行数据库编程
- 查找一个类的好方法,可节省做很多层的传递
- My Sql 语法收集(待续...)
- 中断、陷阱、软中断之间的异同
- Android控件组合应用五
- Android控件组合应用六