Vb.net在目录中搜索具有的文件

Vb.net在目录中搜索具有的文件,vb.net,Vb.net,这是我的目录 Dim strFldCustom As String strFldCustom = "W:\CUSTOM\130000" 我需要搜索此目录子文件夹中的excel文件,然后将其打开。excel文件名为RPTwessummary 132788-03-r1 谢谢您可以使用 System.Directory.EnumerateDirectories(string path) 获取所有子目录的枚举,然后 System.IO.File.Exists() 检查该文件是否存在 您可以使用

这是我的目录

Dim strFldCustom As String
strFldCustom = "W:\CUSTOM\130000"
我需要搜索此目录子文件夹中的excel文件,然后将其打开。excel文件名为RPTwessummary 132788-03-r1

谢谢

您可以使用

System.Directory.EnumerateDirectories(string path)

获取所有子目录的枚举,然后

System.IO.File.Exists()

检查该文件是否存在

您可以使用打开文件

System.Diagnostics.Process.Start()

在VB.Net中的每个链接上都有代码示例


注意:如果文件不在strFldCustom下的文件夹中,而可能存在包含文件夹的文件夹,则可以使用递归在文件夹中搜索文件。

这里是工作基本代码:

F参数是要扫描system的顶级目录。GetFileSystemEntries将负责为您创建递归作业:)

在本例中,您必须在windows文件夹中扫描并传递c:/windows,它将检入所有嵌套而非嵌套的目录,并返回一个字符串数组(),其中包括完整路径:),仅此而已

  Private Sub GetFile(ByVal f As String)
    Try
        Console.WriteLine("Scanning")
        Dim fList = Directory.GetFileSystemEntries(f, "*.xls", SearchOption.AllDirectories).Where(Function(X) X.Contains("rptWESSSummary 132788-03-r1.xls"))
        Dim Z As String = String.Empty
        For Each el In fList
            Z = el.ToString
            Console.WriteLine(Z)
        Next
        Console.WriteLine("Load excel")
        Dim xlApp As New Excel.Application
        xlApp.Workbooks.Open(Z)
        xlApp.Visible = True
        Console.WriteLine("File aperto in excel")
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

    Console.ReadLine()
End Sub

Get all file with directory.GetFileSystem尝试创建一个新的excel应用程序访问默认工作簿并打开您的文件。仅此而已:)希望它有帮助

可能重复您的问题,因为它与此处的许多其他问题重复,而且您没有主动解决问题。所以你不应该让别人帮你做这项工作。您忘记提到枚举目录的递归。@Neolik:问题不是说嵌套子文件夹;-)我将更新以提及这种可能性。您已经完美地执行了需求。我只是觉得OP可能想把这些作为他们的下一个问题来扩展…:)