请问在VB6.0中,怎样读写和生成.ini配置文件?
请问在VB6.0中,怎样读写和生成.ini配置文件?请给也详细的代码并作出解释,谢谢!老大,能不能给点中文注解啊,我很菜啊。...
请问在VB6.0中,怎样读写和生成.ini配置文件?请给也详细的代码并作出解释,谢谢!
老大,能不能给点中文注解啊,我很菜啊。 展开
老大,能不能给点中文注解啊,我很菜啊。 展开
1个回答
展开全部
给你一个类,专门读写INI的
classIniFile.cls:
Option Explicit
Private strINI As String
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
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 Long, ByVal lpFileName As String) As Long
Private Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String
Do While Right$(strDrv, 1) = "\"
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop
Do While Left$(strDir, 1) = "\"
strDir = Mid$(strDir, 2)
Loop
MakePath = strDrv & "\" & strDir
End Function
Private Sub CreateIni(strDrv As String, strDir As String)
strINI = MakePath(strDrv, strDir)
End Sub
Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub
Public Function GetIniKey(strSection As String, strKey As String) As String
Dim strTmp As String
Dim lngRet As String
Dim I As Integer
Dim strTmp2 As String
strTmp = String$(1024, Chr(32))
lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
strTmp = Trim(strTmp)
strTmp2 = ""
For I = 1 To Len(strTmp)
If Asc(Mid(strTmp, I, 1)) <> 0 Then
strTmp2 = strTmp2 + Mid(strTmp, I, 1)
End If
Next I
strTmp = strTmp2
GetIniKey = strTmp
End Function
Public Property Let INIFileName(ByVal New_IniPath As String)
strINI = New_IniPath
End Property
Public Property Get INIFileName() As String
INIFileName = strINI
End Property
Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Function
Public Function DelIniSec(ByVal SectionName As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
End Function
示例:
Dim myIni As New classIniFile
Const myinifile = "\mail.ini"
myIni.INIFileName = App.Path & myinifile
myIni.WriteIniKey "mail", "pop3", pop3
classIniFile.cls:
Option Explicit
Private strINI As String
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
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 Long, ByVal lpFileName As String) As Long
Private Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String
Do While Right$(strDrv, 1) = "\"
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop
Do While Left$(strDir, 1) = "\"
strDir = Mid$(strDir, 2)
Loop
MakePath = strDrv & "\" & strDir
End Function
Private Sub CreateIni(strDrv As String, strDir As String)
strINI = MakePath(strDrv, strDir)
End Sub
Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub
Public Function GetIniKey(strSection As String, strKey As String) As String
Dim strTmp As String
Dim lngRet As String
Dim I As Integer
Dim strTmp2 As String
strTmp = String$(1024, Chr(32))
lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
strTmp = Trim(strTmp)
strTmp2 = ""
For I = 1 To Len(strTmp)
If Asc(Mid(strTmp, I, 1)) <> 0 Then
strTmp2 = strTmp2 + Mid(strTmp, I, 1)
End If
Next I
strTmp = strTmp2
GetIniKey = strTmp
End Function
Public Property Let INIFileName(ByVal New_IniPath As String)
strINI = New_IniPath
End Property
Public Property Get INIFileName() As String
INIFileName = strINI
End Property
Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Function
Public Function DelIniSec(ByVal SectionName As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
End Function
示例:
Dim myIni As New classIniFile
Const myinifile = "\mail.ini"
myIni.INIFileName = App.Path & myinifile
myIni.WriteIniKey "mail", "pop3", pop3
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询