【使用说明】
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():
2 | private staticexternlong WritePrivateProfileString(stringsection, string key, string val, stringfilePath); |
参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
C#申明INI文件的读操作函数GetPrivateProfileString():
2 | private staticexternint GetPrivateProfileString(stringsection, string key, string def, StringBuilder retVal,intsize, stringfilePath); |
参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
【调试截图】
图一、

图二、

图三、

【代码展示(IniClass.cs)】
02 | using System.Collections.Generic; |
04 | using System.Runtime.InteropServices; |
08 | namespace IniFileOperation.Web |
13 | [DllImport("kernel32")] |
14 | privatestaticextern long WritePrivateProfileString(stringsection,string key, stringval, stringfilePath); |
15 | [DllImport("kernel32")] |
16 | privatestaticextern int GetPrivateProfileString(stringsection,string key, stringdef, StringBuilder retVal,intsize, stringfilePath); |
21 | /// <param name="IniPath">文件路径</param> |
22 | publicIniClass(stringIniPath) |
31 | /// <param name="Section">项目名称(如 [TypeName] )</param> |
32 | /// <param name="Key">键</param> |
33 | /// <param name="Value">值</param> |
34 | /// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符。</returns> |
35 | publiclongIniWriteValue(stringSection,string Key, stringValue) |
37 | returnWritePrivateProfileString(Section, Key, Value,this.inipath); |
43 | /// <param name="Section">项目名称(如 [TypeName] )</param> |
44 | /// <param name="Key">键</param> |
45 | publicstringIniReadValue(stringSection,string Key) |
47 | StringBuilder temp =newStringBuilder(500); |
48 | inti = GetPrivateProfileString(Section, Key,"", temp, 500,this.inipath); |
49 | returntemp.ToString(); |
55 | /// <returns>布尔值</returns> |
56 | publicboolExistIniFile() |
58 | returnFile.Exists(inipath); |
【代码展示(Default.aspx.cs)】
02 | using System.Collections.Generic; |
05 | using System.Web.UI.WebControls; |
07 | namespace IniFileOperation.Web |
09 | publicpartialclass _Default : System.Web.UI.Page |
11 | protectedvoidPage_Load(objectsender, EventArgs e) |
19 | /// <param name="sender"></param> |
20 | /// <param name="e"></param> |
21 | protectedvoidButton1_Click(objectsender, EventArgs e) |
24 | stringiniPath = @TextBox1.Text.ToString().Trim(); |
26 | IniClass iniClass =newIniClass(iniPath); |
28 | if(iniClass.ExistIniFile()) |
30 | Response.Write("ini文件存在!路径是:"+ iniPath); |
34 | Response.Write("路径为:"+ iniPath +"处不存在该配置文件。"); |
43 | /// <param name="sender"></param> |
44 | /// <param name="e"></param> |
45 | protectedvoidButton2_Click(objectsender, EventArgs e) |
48 | stringiniPath = @TextBox1.Text.ToString().Trim(); |
50 | IniClass iniClass =newIniClass(iniPath); |
51 | Response.Write("ini文件第一次的写入字节数量为:"+ iniClass.IniWriteValue("SYSDNSection","SYSDNKey1","SYSDNValue1") +"<br/>"); |
52 | Response.Write("ini文件第二次的写入字节数量为:"+ iniClass.IniWriteValue("SYSDNSection","SYSDNKey2","SYSDNValue2") +"<br/>"); |
60 | /// <param name="sender"></param> |
61 | /// <param name="e"></param> |
62 | protectedvoidButton3_Click(objectsender, EventArgs e) |
65 | stringiniPath = @TextBox1.Text.ToString().Trim(); |
67 | IniClass iniClass =newIniClass(iniPath); |
69 | Label1.Text = iniClass.IniReadValue("SYSDNSection","SYSDNKey1")+"<br/>"; |
70 | Label1.Text += iniClass.IniReadValue("SYSDNSection","SYSDNKey2"); |