【已解决,请版主改为已解决】如何获得exe完整路径
本帖最后由 ccxw1983 于 2010-3-12 16:19 编辑在系统环境变量%path%下面的exe可以直接运行,如cmd这样可以运行:
IEXPLORE.EXE "http://www.baidu.com"
请问对于这类exe文件,如何获得完整的路径?
不要用进程法获得完整路径。
我想可以读取环境变量path里面的目录,逐一查找,但是这样好笨的,有没有更加简洁的办法。
附上代码:#include <WinAPI.au3>
#include <Array.au3>
MsgBox(0, "", getFullPathOfFileWhichShortNameExecutable("Explorer.exe"))
MsgBox(0, "", getFullPathOfFileWhichShortNameExecutable("IEXPLORE.EXE"))
MsgBox(0, "", getFullPathOfFileWhichShortNameExecutable("notepad.EXE"))
MsgBox(0, "", getFullPathOfFileWhichShortNameExecutable("SciTE.exe"))
;获得短文件名可执行的文件的完整路径,从环境变量%path%和注册表查找
Func getFullPathOfFileWhichShortNameExecutable($exe_name)
Local $r = getExeFullPathFormPathEnv($exe_name)
If $r <> "" Then
Return $r
Else
Return findFullPathFormRegFoldRecursive($exe_name)
EndIf
EndFunc ;==>getFullPathOfFileWhichShortNameExecutable
;迭代所有目录查找值中含 $name 字样的
;MsgBox(0, "", findFullPathFormRegFoldRecursive("EXCEL.EXE","HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\EXCEL.EXE"))
;MsgBox(0, "", findFullPathFormRegFoldRecursive("EXCEL.EXE"))
Func findFullPathFormRegFoldRecursive($exe_name, $reg_fold = "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications")
If regFoldKey_ValueIsNotEmpty($reg_fold) Then
Local $rsy = findFullPathFormRegFold($reg_fold, $exe_name)
If $rsy <> "" Then Return $rsy
EndIf
If regFoldHasChildFold($reg_fold) Then
Local $i1 = 1
While 1
Local $fold_name_key = RegEnumKey($reg_fold, $i1)
If @error <> 0 Then
ExitLoop
Else
;ConsoleWrite("$file_key:" & $fold_name_key& @CRLF)
Local $newfold = $reg_fold & "\" & $fold_name_key
;MsgBox(0,"",$newfold)
Local $rsx = findFullPathFormRegFoldRecursive($exe_name, $newfold)
If $rsx <> "" Then Return $rsx
EndIf
;Sleep(500)
$i1 = $i1 + 1
WEnd
EndIf
Return ""
EndFunc ;==>findFullPathFormRegFoldRecursive
;在目录枚举值,找到值中含 $name 字样的
Func findFullPathFormRegFold($reg_fold, $exe_name)
$exe_name = StringLower($exe_name)
Local $i = 1
While 1
Local $file_key = RegEnumVal($reg_fold, $i)
If @error <> 0 Then ExitLoop
;ConsoleWrite("$file_key:"&$file_key&@CRLF)
Local $file_value = RegRead($reg_fold, $file_key)
Local $idx = StringInStr($file_value, "\" & $exe_name)
If $idx > 0 Then
$file_value = StringLeft($file_value, $idx + StringLen($exe_name))
$file_value = replaceEnvAsRealPath($file_value)
$file_value = StringReplace($file_value, '"', '')
Return $file_value
EndIf
$i = $i + 1
WEnd
Return ""
EndFunc ;==>findFullPathFormRegFold
;目录有 键-值
;MsgBox(0,"",regFoldKey_ValueIsNotEmpty("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\EXCEL.EXE"))
Func regFoldKey_ValueIsNotEmpty($reg_fold)
RegEnumVal($reg_fold, 1)
Return @error = 0
EndFunc ;==>regFoldKey_ValueIsNotEmpty
;目录有子目录
Func regFoldHasChildFold($reg_fold)
RegEnumKey($reg_fold, 1)
Return @error = 0
EndFunc ;==>regFoldHasChildFold
;替换环境变量为正常字符
;MsgBox(0,"",getRealPathEnv("%ClassPath%"))
Func replaceEnvAsRealPath($pathx)
Local $array = StringRegExp($pathx, "%([^%]+)%", 1)
If @error = 0 Then
For $i = 0 To UBound($array) - 1
Local $oldstr = $array[$i]
Local $newstr = EnvGet($oldstr)
If $newstr <> "" Then
$pathx = StringReplace($pathx, "%" & $oldstr & "%", $newstr)
EndIf
Return $pathx
Next
MsgBox(0, "", $array)
Return ""
Else
Return $pathx
EndIf
EndFunc ;==>replaceEnvAsRealPath
;test_regFoldRegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\EXCEL.EXE\shell\edit")
Func test_regFoldRegEnumKey($reg_fold)
Local $i1 = 1
While 1
Local $key = RegEnumKey($reg_fold, $i1)
If @error <> 0 Then
ExitLoop
Else
ConsoleWrite("$key:" & $key & @CRLF)
EndIf
Sleep(500)
$i1 = $i1 + 1
WEnd
EndFunc ;==>test_regFoldRegEnumKey
;test_regFoldRegEnumVal("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\EXCEL.EXE\shell\edit")
Func test_regFoldRegEnumVal($reg_fold)
Local $i1 = 1
While 1
Local $val = RegEnumVal($reg_fold, $i1)
If @error <> 0 Then
ExitLoop
Else
ConsoleWrite("$val:" & $val & @CRLF)
EndIf
Sleep(500)
$i1 = $i1 + 1
WEnd
EndFunc ;==>test_regFoldRegEnumVal
;在环境变量里面的 %PATH% 指定的目录找 exe 文件
Func getExeFullPathFormPathEnv($exe_name)
Local $var = EnvGet("PATH")
Local $ay = StringSplit($var, ";")
Local $rs = _WinAPI_FindExecutable($exe_name, @ScriptDir)
If $rs <> "" Then
;MsgBox(0, $exe_name, $patha)
Return $rs
EndIf
For $i = 1 To $ay
Local $patha = $ay[$i]
If StringInStr($patha, ":") > 0 Then
;ConsoleWrite($patha & @CRLF)
$rs = _WinAPI_FindExecutable($exe_name, $patha)
If $rs <> "" Then
;MsgBox(0, $exe_name, $patha)
Return $rs
EndIf
EndIf
Next
Return ""
EndFunc ;==>getExeFullPathFormPathEnv
回复 1# ccxw1983
@AutoItExe 注册表中有相关位置存储本机的可直接执行的 EXE 或COM
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications
用AU3中注册表相关的命令就可以获取到! 谢谢 maxkingmax 提示
页:
[1]