获取所选文件的完整路径

时间:2014-05-06 02:41:24

标签: vb.net

我正在尝试获取所定位文件的完整路径。我正在搜索目录,当找到文件时,我需要得到它的路径。我有所有要搜索的代码,我只需要它来设置字符串或获取文件的路径。我怎么能这样做?

Dim substring As String = strJobNumber.Substring(0, 2)
Dim directory As String
Dim strFileName As String = "rptWESSSummary " & strJobNumber & "-" & strLineNumber & "-r1.xlsx"

If substring = "13" Then
    directory = "W:\CUSTOM\130000\" & strJobNumber
    For Each filename As String In IO.Directory.GetFiles(directory, "*", IO.SearchOption.AllDirectories)
        fName = IO.Path.GetFileName(filename)
        If fName = strFileName Then
            ' This is where I need the path of the file if it’s found.
            MsgBox("File found!!!!!!!!!")
        End If
    Next

1 个答案:

答案 0 :(得分:0)

为目录创建新的DirectoryInfo;它会给你FileInfo个对象:

For Each file In New IO.DirectoryInfo(directory).GetFiles("*", IO.SearchOption.AllDirectories)
    If file.Name = strFileName Then ' Please don’t use Hungarian notation
        MessageBox.Show("File found!")
        ' Do something with file.FullName
    End If
Next

如果Name真的是你正在做的唯一检查,为什么不首先搜索它?

Dim directory As New IO.DirectoryInfo("W:\CUSTOM\130000\" & strJobNumber)

For Each file In directory.GetFiles(strFileName, IO.SearchOption.AllDirectories)
    ' Do something with file.FullName
Next