Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Command1_Click()
ShellExecute Me.hwnd, "open", App.Path & "\Project1.vbp", "", "", 0
End Sub
Option Explicit
Const MAX_FILENAME_LEN = 260
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Sub Command1_Click()
On Error Resume Next
Dim FindReturn As Integer, StrBuf As String, FileName As String, StrFound As String
FileName = App.Path & "\Test.vbp"
Open FileName For Output As #1 'vb的工程后缀为vbp,VC的为dsw
Close #1
StrBuf = String(MAX_FILENAME_LEN, 32)
If Dir(FileName) <> "" Then
FindReturn = FindExecutable(FileName, vbNullString, StrBuf)
If FindReturn > 32 Then
StrFound = Left(StrBuf, InStr(StrBuf, Chr(0)) - 1)
MsgBox StrFound
Shell StrFound
Else
MsgBox "没有找到与指定文件相关联的程序"
End If
Kill FileName
End If
End Sub
VB声明
Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
说明
查找与一个指定文件关联在一起的程序的文件名。可用Windows注册表编辑器将文件类型与特定的应用程序关联到一起。比如,扩展名为.TXT的文本文件通常与Windows记事本(Notepad.exe)关联到一起。如在文件管理器中双击含.TXT扩展名的一个文件,会自动启动记事本,并在其中载入文本文件
返回值
Long,大于32表示成功;31表示不存在文件类型的关联;0表示系统内存或资源不足;ERROR_FILE_NOT_FOUND表示指定的文件不存在;ERROR_PATH_NOT_FOUND表示指定的路径不存在;ERROR_BAD_FORMAT表示执行格式无效
参数表
参数 类型及说明
lpFile String,指定要为其查找相关程序的一个文件名或程序名
lpDirectory String,要使用的默认目录的完整路径
lpResult String,指定一个字串缓冲区,用于装载可执行程序的名字。注意这个字串预先至少都要初始化成MAX_PATH个字符的长度