VB.NET做为一款***的开发平台,为开发人员带来了很大的帮助。因为对XML前不了解,所以对XML方式来做配置文件我都不能很好的实现,但为了应行,只有先使用INI的文来记录了,也就沿用了VB6里的INI文读取方法。#t#
下面请看VB.NET读取INI文件的相关代码示例:
- '声明INI配置文件读写API函数
- Private Declare Function GetPrivateProfileString()
Function GetPrivateProfileString Lib "kernel32"
Alias "GetPrivateProfileStringA" (ByVal
lpApplicationName As String, ByVal lpKeyName As
String, ByVal lpDefault As String, ByVal lpReturned
String As String, ByVal nSize As Int32, ByVal
lpFileName As String) As Int32 - Private Declare Function WritePrivateProfileString()
Function WritePrivateProfileString Lib "kernel32"
Alias "WritePrivateProfileStringA" (ByVal
lpApplicationName As String, ByVal lpKeyName As
String, ByVal lpString As String, ByVal lpFileName
As String) As Int32 - '定义读取配置文件函数
- Public Function GetINI()Function GetINI(ByVal
Section As String, ByVal AppName As String, ByVal
lpDefault As String, ByVal FileName As String)
As String - Dim Str As String = LSet(Str, 256)
- GetPrivateProfileString(Section, AppName,
lpDefault, Str, Len(Str), FileName) - Return Microsoft.VisualBasic.Left(Str, InStr
(Str, Chr(0)) - 1) - End Function
- '定义写入配置文件函数
- Public Function WriteINI()Function WriteINI
(ByVal Section As String, ByVal AppName As
String, ByVal lpDefault As String, ByVal
FileName As String) As Long - WriteINI = WritePrivateProfileString(Section,
AppName, lpDefault, FileName) - End Function
- Private Sub Form1_Load()Sub Form1_Load(ByVal
sender As System.Object, ByVal e As System.
EventArgs) Handles MyBase.Load - Dim path As String
- path = Application.StartupPath + "\server.ini"
- TextBox1.Text = GetINI("Server", "IP", "", path)
- TextBox2.Text = GetINI("Server", "port", "", path)
- End Sub
- Private Sub Button1_Click()Sub Button1_Click
(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click - Try
- Dim path As String
- path = Application.StartupPath + "\server.ini"
- WriteINI("Server", "IP", TextBox1.Text, path)
- WriteINI("Server", "port", TextBox2.Text, path)
- MsgBox("配置设置已经成功!!!!")
- Me.Close()
- Catch ex As Exception
- MsgBox("错误!!!!")
- End Try
- End Sub
VB.NET读取INI文件的相关操作就为大家介绍到这里。