[VB.NET]读写INI文件

来源:互联网 发布:linux 更改home空间 编辑:程序博客网 时间:2024/05/01 09:14
读写INI文件

实例说明

在本实例中,我们将应用VB.NET制作一个能够实现读写INI文件的应用程序。程序运行结果如图67-1所示。

图67-1 运行结果

技术要点

<script type="text/javascript"><!--google_ad_client = "pub-8333940862668978";/* 728x90, 创建于 08-11-30 */google_ad_slot = "4485230109";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

l INI文件的格式

l GetPrivateProfileInt()和GetPrivateProfileString()函数

l WritePrivateProfileString()函数

实现步骤

■ 新建项目

打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中,选择"Windows应用程序",在名称域中输入"RWIni",然后选择保存路径。单击"确认"。

■ 添加控件和菜单

向当前窗体上添加两个Button控件,用于控制读写INI文件;一个Group控件和两个RadioButton控件,用于控制读写整型数据还是字符串型;三个Label控件和三个TextBox控件,用于标注和输入节名、键名和值;其余两个Label控件,一个表示当前打开的文件名,另一个表示读写的状态。最后添加一个MainMenu控件,生成菜单"文件"、"退出",其中"文件"下有一个子菜单"打开INI文件"。

■ 设置属性

切换到"属性栏",对窗体上的控件设置属性。详细情况见表67-1。

表67-1 窗体各控件的属性值

窗体/控件 属性 值

optInt value True

Lablel5 Name Lblfilename

Autosize true

BackSytle 0-Transparent

Button1 Text 读取

BackColor &H0080C0FF

Style 1-Graphic

TextBox1 Name txtSection

Text (空)

知识点:一个INI文件由若干节(Section)组成,每个节中又由若干个关键字(Keyword)和值组成。关键字是用来保存独立的程序设置和那些重要信息的,用于控制应用程序的某个功能;值可以是整型数或字符串。如果值为空,则表示应用程序已经为该关键字指定了缺省值。

INI文件的一般形式如下:

…………

[Section]

keyword=value

…………

INI文件有严格的格式要求:

(1)节名必须加"["和"]"。

(2)对文件做注释,要以";"开头。

(3)关键字后必须有"="。

■ 添加代码

keywordinfo = txtkeyword.Text

Valueinfo = txtvalue.Text

' 检查输入是否合法,不合法时,提示警告信息。

' 读取INI文件的内容

Private Sub Button1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button1.Click

Dim readinfo As String

Dim buffer As VB6.FixedLengthString = New VB6.FixedLengthString(255)

Dim Sectioninfo As String

Dim Valueinfo As String

Dim keywordinfo As String

Sectioninfo = txtsection.Text

If Trim(Sectioninfo) = "" Then ' 在此可以用len(Sectioninfo)=0来代替

MsgBox("请输入节名(Section)", MsgBoxStyle.OKOnly, "错误信息")

Exit Sub

End If

If Trim(keywordinfo) = "" Then

MsgBox("请输入关键字(Keyword)", MsgBoxStyle.OKOnly, "错误信息")

Exit Sub

End If

' 判断读取的是数字还是字符串

If optint.Checked Then

' 使用 GetPrivateProfileInt函数取回一个关键字的整型数值

readinfo = CStr(GetPrivateProfileInt(Sectioninfo, keywordinfo, -1, AXCommonDialog1.FileName))

' GetPrivateProfileInt函数返回取得的值,如果键没有找到,则返回由nDefault参数指定的值

If CDbl(readinfo) = -1 Then

txtvalue.Text = "读取失败!"

Else

txtvalue.Text = readinfo

End If

Else

' 使用 GetPrivateProfileString函数取回一个关键字的字符串数值

readinfo = CStr(GetPrivateProfileString(Sectioninfo, keywordinfo, "没有找到相对应的键值", buffer.Value, 255, AXCommonDialog1.FileName))

' GetPrivateProfileString函数返回放入lpReturnedString缓冲区的字节数,

' 键值放在由参数lpReturnedString指定的缓冲区中。该缓冲区是一个由nSize字节大小的固定字符串

If readinfo = "没有找到相对应的键值" Then

txtvalue.Text = "读取失败!"

Else

txtvalue.Text = VB.Left(buffer.Value, CInt(readinfo))

End If

End If

End Sub

' 写入INI文件

Private Sub Button2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button2.Click

Dim writeinfo As Boolean

Dim buffer As VB6.FixedLengthString = New VB6.FixedLengthString(255)

Dim Sectioninfo As Object

Dim Valueinfo As String

Dim keywordinfo As String

Sectioninfo = txtsection.Text

keywordinfo = txtkeyword.Text

Valueinfo = txtvalue.Text

' 使用更新给定节中的新键值,或创建一个新键值,

' 或用来删除一个节或键值

writeinfo = WritePrivateProfileString(Sectioninfo, keywordinfo, Valueinfo, AXCommonDialog1.FileName)

' 注意,WritePrivateProfileString函数设置成功,返回值为True,否则返回False

If writeinfo = True Then

Label4.Text = "写入INI文件成功!"

Else

Label4.Text = "错误!不能写入INI文件!"

End If

End Sub

' 打开INI文件

Public Sub mnuOpenINIFile_Popup(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mnuOpenINIFile.Popup

mnuOpenINIFile_Click(eventSender, eventArgs)

End Sub

Public Sub mnuOpenINIFile_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mnuOpenINIFile.Click

' 设置过滤器

AXCommonDialog1.Filter = "*.INI|*.ini"

AXCommonDialog1.ShowOpen()

If Len(AXCommonDialog1.FileName) = 0 Then Exit Sub

' 打开有效文件后,按钮可用

Button1.Enabled = True

Button2.Enabled = True

' 显示当前打开的文件名

lblfilename.Text = "现在打开的文件是:" & AXCommonDialog1.FileName

End Sub

■ 运行程序

单击菜单"调试|启动"或单击 图标运行程序。

小结

INI文件是包含了应用程序特定信息的文本文件。比如将用户所作的选择以及各种变化的系统信息记录在INI文件中,通常是在应用程序运行时读取INI文件中的信息,退出应用程序时保存用户对运行环境的某些修改。

原创粉丝点击