【使用说明】
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 static extern long WritePrivateProfileString( string section, string key, string val, string filePath); |
参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
C#申明INI文件的读操作函数GetPrivateProfileString():
2 | private static extern int GetPrivateProfileString( string section, string key, string def, StringBuilder retVal, int size, string filePath); |
参数说明: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 | 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); |
21 | /// <param name="IniPath">文件路径</param> |
22 | public IniClass( string IniPath) |
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) |
37 | return WritePrivateProfileString(Section, Key, Value, this .inipath); |
43 | /// <param name="Section">项目名称(如 [TypeName] )</param> |
44 | /// <param name="Key">键</param> |
45 | public string IniReadValue( string Section, string Key) |
47 | StringBuilder temp = new StringBuilder(500); |
48 | int i = GetPrivateProfileString(Section, Key, "" , temp, 500, this .inipath); |
49 | return temp.ToString(); |
55 | /// <returns>布尔值</returns> |
56 | public bool ExistIniFile() |
58 | return File.Exists(inipath); |
【代码展示(Default.aspx.cs)】
02 | using System.Collections.Generic; |
05 | using System.Web.UI.WebControls; |
07 | namespace IniFileOperation.Web |
09 | public partial class _Default : System.Web.UI.Page |
11 | protected void Page_Load( object sender, EventArgs e) |
19 | /// <param name="sender"></param> |
20 | /// <param name="e"></param> |
21 | protected void Button1_Click( object sender, EventArgs e) |
24 | string iniPath = @TextBox1.Text.ToString().Trim(); |
26 | IniClass iniClass = new IniClass(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 | protected void Button2_Click( object sender, EventArgs e) |
48 | string iniPath = @TextBox1.Text.ToString().Trim(); |
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/>" ); |
60 | /// <param name="sender"></param> |
61 | /// <param name="e"></param> |
62 | protected void Button3_Click( object sender, EventArgs e) |
65 | string iniPath = @TextBox1.Text.ToString().Trim(); |
67 | IniClass iniClass = new IniClass(iniPath); |
69 | Label1.Text = iniClass.IniReadValue( "SYSDNSection" , "SYSDNKey1" )+ "<br/>" ; |
70 | Label1.Text += iniClass.IniReadValue( "SYSDNSection" , "SYSDNKey2" ); |