欢迎来到Java学习网!
设为首页 | 加入收藏
当前位置: 首页 > 编程开发 > VB语言 >

使用VB时经常会保存一些窗体或其他控件的配置。读取INI信息是一种很方便的做法。以下是代码!

Option Explicit

'获取和修改INI文件
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
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 Long

'读取INI文件
Private Function GetINI(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String) As String
Dim S As String
S = String(32767, " ")
GetPrivateProfileString lpApplicationName, lpKeyName, lpDefault, S, 32767, App.Path & "\Config.INI"
GetINI = Left(S, InStr(S, Chr(0)) - 1)
End Function


'保存INI文件
Private Function SetINI(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String) As Long
WritePrivateProfileString lpApplicationName, lpKeyName, lpString, App.Path & "\Config.INI"
End Function


Private Sub Command1_Click() '保存设置

SetINI "Settings", "vbTest", vbTest.text
SetINI "Settings", "wzxwolf", wzxwolf.text

End Sub

 

Private Sub Check2_Click() '读取设置

vbTest = GetINI("Settings", "vbTesT", "默认字符串")
wzxwolf = GetINI("Settings", "wzxwolf", "默认字符串")

End Sub

    相关教程
    推荐内容