比如:我知道某一启动程序名为:**.EXE,但我想得到它在本机中的全路径名,该调用什么API啊?
SHELL调用EXE程序应该是全路径名吧,另外如果想启动一个**.EXE程序,可以用什么更好的方法?

解决方案 »

  1.   

    一. 利用DIR函数查找文件 ---- Dir[(pathname[, attributes])]是VB提供的用来检查某些文件或目录是否存在的函数,它返回一个 String,用以表示一个文件名、目录名或文件夹名称,返回值必须与指定的模式或文件属性、或磁盘卷标相匹配。 ---- 如果文件的路径很清楚,那么确定文件是否存在简单地调用该函数就行了。如果光知道文件名,甚或只知道文件的后缀,要定位文件的话就需要一定的编码了。下面的例子用来定位c:\下所有目录内是否有文件Win.ini。 Function FindDirectory(RootPath As 
    String, Mydirectory() As String)
    Dim intResult, I, intFind As Integer
    ‘首先查找根目录下的所有子目录
    MyPath = "c:\" ' 指定路径c:\。
    MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。
    intResult = 1
    ReDim Mydirectory(intResult) ‘初始化动态数组
    Do While MyName < > "" ' 开始循环。
     ' 跳过当前的目录及上层目录。
     If MyName < > "." And MyName < > ".." Then
     ' 使用位比较来确定 MyName 代表一目录。
     If (GetAttr(MyPath & MyName) 
     And vbDirectory) = vbDirectory Then
     
     ' 如果它是一个目录,将其名称存储在一个数组里。
     Mydirectory(intResult) = MyPath & MyName
     intResult = intResult + 1
     ReDim Preserve Mydirectory(intResult) 
    ‘分配动态数组实际的元素个数,并保留数组中的数据
     End If
     End If
     MyName = Dir ' 查找下一个目录。
    Loop
    ‘在所有目录里分别查找文件是否存在。
    For I = 1 To UBound(Mydirectory)-1
     MyFile = Mydirectory(I) & "\win.ini"
     intFind = Len(Dir(MyFile))
     If intFind < > 0 Then MsgBox "找到文件" &
     Dir(MyFile) & "在:" & Mydirectory(I)
    Next I
    End Function---- 该函数的思路很清晰:先遍历根目录下所有的子目录,然后在每个子目录里查找文件。该函数有一个缺陷:只能查找根目录下的一级子目录,无法遍及整个硬盘。如果要遍及整个硬盘,还需要额外的编码,这里不再多述。 ---- 二. 利用API函数查找文件 ---- 在使用VB的过程中我深深地体会到,只有充分利用API的函数才能更充分地发挥VB的优势。 API函数 SearchTreeFile可以很快地定位一个文件。借助该函数笔者编了一个快速查找文件的函数sysFileFind. Declare
    Public Declare Function SearchTreeForFile Lib
     "imagehlp.dll" (ByVal lpRoothPath As String, 
    ByVal lpInputName As String, 
    ByVal lpOutputName As String) As Long---- 下面为sysFileFind函数的编码: Public Function sysFileFind
    (ByVal WhichRootPath As String,
     ByVal WhichFileName As String) As String
    Dim iNull As Integer
    Dim lResult As Long
    Dim sBuffer As String
     On Error GoTo L_FILEFINDERROR
     sBuffer = String$(1024, 0)
     '查找文件
    lResult = SearchTreeForFile
    (WhichRootPath, WhichFileName, sBuffer)
     '如果文件找到,将返回字符串后续的空格删除
     '否则返回一个空字符串
     If lResult Then
     iNull = InStr(sBuffer, vbNullChar)
     If Not iNull Then
      sBuffer = Left$(sBuffer, iNull - 1)
     End If
     sysFileFind = sBuffer
     Else
      sysFileFind = ""
     End If
    Exit Function
    L_FILEFINDERROR:
    MsgBox "查找文件过程中遇到错误!",
     vbInformation, "查找文件错误"
    sysFileFind = Format(Err.Number) 
     & " - " & Err.Description
    End Function---- 该函数可以很快遍历整个硬盘,从而查找到我们所需的文件。 
      

  2.   

    用FileScriptObject搜索http://blog.csdn.net/lxcc/archive/2004/10/23/148796.aspx改改文件夹,和完整的文件名称,而不是扩展名