读书人

从ini资料获取路径然后从这个路径获

发布时间: 2013-07-08 14:13:00 作者: rapoo

从ini文件获取路径,然后从这个路径获取文件后缀名,肿出错求指教啊
从ini文件获取路径,然后从这个路径获取文件后缀名,肿出错,求指教

代码部分



'ini调用的API
Private Declare Function GetPrivateProfileString Lib _
"kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
lpKeyName As Any, ByVal lpDefault As String, ByVal lpretunedstring 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

'读INI文件函数
Public Function GetFromINI(Appname As String, Keyname As String, _
FileName As String) As String
Dim RetStr As String
RetStr = String(255, Chr(0))
GetFromINI = Left(RetStr, GetPrivateProfileString(Appname, ByVal Keyname, "", RetStr, Len(RetStr), FileName))
End Function
'**************************************************************

'写INI文件函
Public Function WriteINI(inipath As String, Appname As String, Keyname As String, value As String)
Dim stemp As String
stemp = value
Call WritePrivateProfileString(Appname, Keyname, stemp, inipath)
End Function

'从全路径中返回文件的扩展名
Public Function strFileShortNameSuf(strFilePath As String) As String
Dim temp
If InStr(strFilePath, ".") = 0 Then Exit Function
temp = Split(strFilePath, ".")
strFileShortNameSuf = temp(UBound(temp))
End Function

Private Sub Command1_Click()
Dim processPath As String, ssName As String
processPath = GetFromINI("程序", "进程路径1", App.Path & "\config.ini")

ssName = strFileShortNameSuf(processPath)
MsgBox ssName '此时弹出窗口显示文件扩展名为exe

If ssName = "exe" Then '结果显示Error
MsgBox "小写 OK"
Else
MsgBox "小写 Error"
End If

If ssName = "EXE" Then '结果显示Error
MsgBox "大写 OK"
Else
MsgBox "大写 Error"
End If

If LCase(ssName) = "exe" Then '结果显示Error
MsgBox "转换小写 OK"
Else
MsgBox "转换小写 Error"
End If
a = WriteINI(App.Path & "\config.ini", "设置", "扩展名", ssName) '注意我把变量ssName值写入ini文件


a = GetFromINI("设置", "扩展名", App.Path & "\config.ini") '从ini文件读取刚才写入的值

If a = "exe" Or a = "EXE" Then '结果显示OK
MsgBox "读ini结果 OK"
Else
MsgBox "读ini结果 Error"
End If

Select Case ssName '这是我最终目的,根据后缀名的不同运行的运行方式
Case "exe"
Shell processPath
Case "vbs"
Shell "explorer " & processPath
Case "bat"
Shell "CMD.exe /c " & processPath
End Select
End Sub



ini文件部分

[程序]
进程路径1=D:\诛仙.exe

[解决办法]
再改一下吧

Public Function GetFromINI(Appname As String, Keyname As String, _
FileName As String) As String
Dim RetStr As String
Dim L As Long

RetStr = String(255, Chr(0))

L = GetPrivateProfileString(Appname, ByVal Keyname, "", RetStr, 255, FileName)
l=instr(1,retstr,chr(0))
GetFromINI = Left(RetStr, L-1)
End Function

读书人网 >VB

热点推荐