扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
VB.NET读写INI配置文件
创新互联建站是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括成都网站建设、网站制作、电商网站制作开发、小程序开发、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path As String
path = Application.StartupPath + "\Send.ini"
TextBox1.Text = GetINI("Send", "Send1", "", path)
TextBox2.Text = GetINI("Send", "Send2", "", path)
Dim IsSms As Integer = GetINI("Send", "IsSms", "", path)
If (IsSms = 1) Then
Me.RadioButton1.Checked = True
ElseIf (IsSms = 0) Then
Me.RadioButton2.Checked = True
End If
End Sub
Public 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(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 Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim path As String
path = Application.StartupPath + "\Send.ini"
WriteINI("Send", "Send1", TextBox1.Text, path)
WriteINI("Send", "Send2", TextBox2.Text, path)
If (Me.RadioButton1.Checked = True) Then
WriteINI("Send", "IsSms", 1, path)
ElseIf (Me.RadioButton2.Checked = True) Then
WriteINI("Send", "IsSms", 0, path)
End If
MsgBox("配置设置已经成功!!!!")
Catch ex As Exception
MsgBox("错误!!!!")
End Try
End Sub
总体思路,当文本文件读,system中有一个io.file对象,可以用readalltext读取txt文件,读完了过后再用string.split函数一个个进行解析,赋值。不过真没听说过INT配置文件,不是INI吗?
使用ConfigurationSettings.AppSetting读取,如下例
Imports System.Configuration
Private ReadOnly db As String = ConfigurationSettings.AppSettings("DB")
设计功能
说明:第一个参考页导入了System.IO
功能sdGetIniInfo(字符串的BYVAL iniSection的字符串,BYVAL INIFILE),如果该字符串/ File.Exists (INIFILE返回的“文件”INIFILE的的)
,而“未找到,以确保路径和文件名是否正确!”如果
黑暗iniRead
昏暗的iniStr新的StreamReader(INIFILE),的字符串iniRead.ReadToEnd
朦胧朦胧的整数
最后的出口,克莱因整数
昏暗的NOSEC布尔=假
作为一个字符的字符串昏暗的getValue“
黑暗的壁橱
衣柜iniStr.Split(CHR(13 ))CLINE = UBOUND功能的(CLST)
= 0 CLINE
衣柜(I)的indexOf(“=”) 0,
衣柜(I)分裂(“=”) (0)TRIM()= iniSection
NOSEC =
的getValue = CLST(I)分裂(“=”;)(1)修剪()
BR /退出
最后,如果
如果NOSEC = TRUE
返回的getValue BR /
的安装信息没有找到“iniSection!”
功能
====如果=================== ===================
TEXT1 = sdGetIniInfo(路径,“名称”)
事实上,无论是ini文本文件XML只使用一定的格式,或所谓的数据结构,在最后的分析中,实现的文件操作的功能只是一个包文件的读取和写入操作。
是不是写一个函数的API!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流