VB怎么读取ini?

我做了个程序和ini,想让VB读取ini,ini内容如下:
[PATH]
CS=/cstrike.exe
GTASA=/gta_sa.exe
samp=/samp.exe
JC2=/JustCause2_game.exe
FC2=/bin/FarCry2.exe

我想让VB的Command1按下后读取ini[PATH]节里的CS的属性,从而利用CS属性打开软件目录下的/cstrike.exe

不是用像这样的代码打开:
Shell App.Path & "/cstrike.exe", vbNormalFocus

设您的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文件的段名称,不用改。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-10
第2个回答  2013-07-10
我是用2005 写的 前面是声明,调用ReadINI过程就可以了
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 Integer, ByVal lpFileName As String) As Integer
Function ReadINI(ByRef Section As String, ByRef KeyName As String, ByRef filename As String) As String
Dim sRet As String
Dim intG As Integer
sRet = New String(Chr(0), 255)
intG = GetPrivateProfileString(Section, KeyName, "", sRet, Len(sRet), filename
ReadINI = Left(sRet, intG)
End Function
第3个回答  2013-07-10
就当作文本文件读取,也可以的!