傳回目標檔案完整路徑

摘要:傳回目標檔案完整路徑

 

 因為一些電腦 各有不同版本的program
所以那些exe檔案的位置有時會不一樣, 不能hard code 它的位置

而 exe的名稱是一樣的

所以 我就開發這個function 來 尋找 exe 的路徑

 


    Public Function sFindFileFullpath(ByVal psDirectory As String, ByVal psTargetFileName As String) As String

        Try

            Dim lsFilepath As String = BLANK
            Dim lFileInfo As System.IO.FileInfo
            Dim lsFileList() As String = System.IO.Directory.GetFiles(psDirectory)

            For j As Integer = 0 To lsFileList.Length - 1
                lFileInfo = New System.IO.FileInfo(lsFileList(j))
                If lFileInfo.Name.ToString().ToUpper().Equals(psTargetFileName.ToUpper) Then
                    'file found
                    Return lFileInfo.FullName.ToString
                    Exit Function
                End If
            Next

            Dim lsDirectoryList() As String = System.IO.Directory.GetDirectories(psDirectory)

            For i As Integer = 0 To lsDirectoryList.Length - 1
                'file not found ,then sFindFileFullpath again
                If lsFilepath = BLANK Then
                    lsFilepath = sFindFileFullpath(lsDirectoryList(i), psTargetFileName)
                End If
            Next

            Return lsFilepath
        Catch err As Exception
            Call ErrHandler(err.Message, APP_NAME)
        End Try

    End Function

 

 

/p = print

lsFileFullPath = sFindFileFullpath("C:\Program Files\Adobe\", "acrord32.exe") + " /p/h"

 

Call Shell(lsFileFullPath & " " & TEMP_FILE_NAME & liCount.ToString & ".pdf", AppWinStyle.MinimizedNoFocus)

 


 傳回目標檔案完整路徑
Find the fullpath of the file in specific folder.

程式一
單層searching method

    Private Function sFindFileFullpath(ByVal psFolder As String, ByVal psTargetFileName As String) As String

        Try

            Dim lsFilepath As String = BLANK
            Dim lFileInfo As System.IO.FileInfo
            Dim lsDirectoryList() As String = System.IO.Directory.GetDirectories(psFolder)

            For i As Integer = 0 To lsDirectoryList.Length - 1
                Dim lsFileList() As String = System.IO.Directory.GetFiles(lsDirectoryList(i))

                For j As Integer = 0 To lsFileList.Length - 1
                    lFileInfo = New System.IO.FileInfo(lsFileList(j))

                    If lFileInfo.Name.ToString().ToUpper().Equals(psTargetFileName) Then
                        'file found
                        lsFilepath = lFileInfo.FullName.ToString
                        sFindFileFullpath = lsFilepath
                        Exit Function
                    End If
                Next
            Next

            'file not found
            sFindFileFullpath = BLANK

        Catch err As Exception
            Call ErrHandler(err.Message, APP_NAME)
        End Try

 

 

 

 

------------------

熱愛生命 喜愛新奇 有趣的事物

 

過去 是無法改變
將來 卻能夠創造

 

希望使大家生活更便利
世界更美好

 

a guy who loves IT and life