VB.NET讀寫INI配置文件
創(chuàng)新互聯(lián)建站是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括成都網(wǎng)站建設(shè)、網(wǎng)站制作、電商網(wǎng)站制作開發(fā)、小程序開發(fā)、微信營銷、系統(tǒng)平臺(tái)開發(fā),與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評(píng)估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!
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("配置設(shè)置已經(jīng)成功?。。。?)
Catch ex As Exception
MsgBox("錯(cuò)誤?。。?!")
End Try
End Sub
總體思路,當(dāng)文本文件讀,system中有一個(gè)io.file對(duì)象,可以用readalltext讀取txt文件,讀完了過后再用string.split函數(shù)一個(gè)個(gè)進(jìn)行解析,賦值。不過真沒聽說過INT配置文件,不是INI嗎?
使用ConfigurationSettings.AppSetting讀取,如下例
Imports System.Configuration
Private ReadOnly db As String = ConfigurationSettings.AppSettings("DB")
設(shè)計(jì)功能
說明:第一個(gè)參考頁導(dǎo)入了System.IO
功能sdGetIniInfo(字符串的BYVAL iniSection的字符串,BYVAL INIFILE),如果該字符串/ File.Exists (INIFILE返回的“文件”INIFILE的的)
,而“未找到,以確保路徑和文件名是否正確!”如果
黑暗iniRead
昏暗的iniStr新的StreamReader(INIFILE),的字符串iniRead.ReadToEnd
朦朧朦朧的整數(shù)
最后的出口,克萊因整數(shù)
昏暗的NOSEC布爾=假
作為一個(gè)字符的字符串昏暗的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(路徑,“名稱”)
事實(shí)上,無論是ini文本文件XML只使用一定的格式,或所謂的數(shù)據(jù)結(jié)構(gòu),在最后的分析中,實(shí)現(xiàn)的文件操作的功能只是一個(gè)包文件的讀取和寫入操作。
是不是寫一個(gè)函數(shù)的API!