设您的INI文件存在于程序目录下名为Settings.ini好了。
VB6.0代码如下,INI文件操作需要调用API。
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 Long, ByVal lpFileName As String) As Long
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 GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
'——————————————————————————————————————————————————————————————————————
Private Sub Command1_Click()
Dim lpReturnedString As String * 50
Dim lpApplicationName As String, lpKeyName As String, lpDefault As String, lpFileName As String
Dim Path As String
lpFileName = App.Path & "\Settings.ini" '指定要进行操作的INI文件
lpApplicationName = "PATH" '设置要读取的段名称(字符串型)
lpKeyName = "CS" '设置要读取的键名称(字符串型)
lpDefault = "Error" '设定当指定的段或键没找到时返回的值
GetPrivateProfileString lpApplicationName, lpKeyName, lpDefault, lpReturnedString, Len(lpReturnedString), lpFileName
If Left(Trim(lpReturnedString), 5) = "Error" Then '判断是否返回错误值
MsgBox "读取INI文件出错,指定值不存在!", vbOKOnly + vbCritical + vbSystemModal, "Error"
Exit Sub
Else
Path = Trim(lpReturnedString) '不出错则将路径交给Path变量
End If
On Error Resume Next '如果接下来的代码出错则忽略错误继续执行。
Shell App.Path & Path, vbNormalFocus
End Sub
还有一个小错误要指出,在系统路径中,只有“\”符号。您的“/”符号在系统里是除号,或者用于网页。
代码已在XP SP3+VB6.0 SP6环境下测试通过。
春风学园 计算机研究社的小柊很高兴为您解答。
追问哈,/号在html里用惯了,那个path变量名是不是可以改呀,就是第12行的
追答当然可以,因为我Path变量用惯了,不过你把Path变量改变了记得把22行和25行的Path也改掉。14行的PATH是你的INI文件的段名称,不用改。