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文件的相关操作就为大家介绍到这里。
当前名称:迅速掌握VB.NET读取INI文件操作方法
本文来源:http://www.shufengxianlan.com/qtweb/news11/338261.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
广告
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源:
创新互联