Vb.net 搜索包含字符串的文件夹并打开

Vb.net 搜索包含字符串的文件夹并打开,vb.net,Vb.net,我正在尝试编写一个非常简单的程序,需要在文本框中输入4位数字,其目标是打开一个以输入的4位数字结尾的文件夹 我目前掌握的守则如下: Private Sub GoBut_Click(sender As System.Object, e As System.EventArgs) Handles GoBut.Click Dim wdcode As String Dim wd_path As String Dim design_drive As String wdc

我正在尝试编写一个非常简单的程序,需要在文本框中输入4位数字,其目标是打开一个以输入的4位数字结尾的文件夹

我目前掌握的守则如下:

 Private Sub GoBut_Click(sender As System.Object, e As System.EventArgs) Handles GoBut.Click

    Dim wdcode As String
    Dim wd_path As String
    Dim design_drive As String

    wdcode = TextCode.Text

    design_drive = "Y:\Sample Code Sequence\"

    wd_path = design_drive & wdcode

    Process.Start("explorer.exe", String.Format("/n, /e, {0}", wd_path))
End Sub
我是VB的新手,所以请保持相当直接。我希望在资源管理器中打开的文件夹长度为8位,将位于子目录中

总之,文件夹和子文件夹的格式如下:

Y:/Sample Code Sequence/1001 - 1100/15061089
Y:/Sample Code Sequence/1001 - 1100/15061090
Y:/Sample Code Sequence/1001 - 1100/15061091
Y:/Sample Code Sequence/1001 - 1100/15071092
....
Y:/Sample Code Sequence/1101 - 1200/15071111
Y:/Sample Code Sequence/1001 - 1100/15071131
etc

8位文件夹名称的最后4位是唯一的。

递归函数可能更适合长期重用,但我将其编写为一个嵌套循环。它获取基本目录(在您的示例中为“Y:\Sample Code Sequence\”),并使用DirectoryInfo类的GetDirectories函数返回基本目录的子目录。然后,它循环遍历这些子目录并执行相同的操作,如果找到匹配项,则返回该目录的完整路径(如果找不到匹配项,则不返回任何内容)


递归函数对于长期的可重用性可能更好,但我将其作为嵌套循环编写。它获取基本目录(在您的示例中为“Y:\Sample Code Sequence\”),并使用DirectoryInfo类的GetDirectories函数返回基本目录的子目录。然后,它循环遍历这些子目录并执行相同的操作,如果找到匹配项,则返回该目录的完整路径(如果找不到匹配项,则不返回任何内容)


您可以使用接受通配符的
My.Computer.FileSystem.GetDirectories
():

Dim found As String = My.Computer.FileSystem.GetDirectories("Y:\Sample Code Sequence\", _
                                      FileIO.SearchOption.SearchAllSubDirectories, _
                                      "*1089").FirstOrDefault()
MessageBox.Show(found)
要应用此方法,需要将要查找的值连接到“*”

您可以添加进一步的检查;例如,要检查文件夹名称是否有8个字符(以1089结尾),请执行以下操作:

这实际上可以通过使用
??1089
进行搜索来简化,其中?是单个字符的通配符

检查文件夹名称是否为8个字符(以4位数字开头),需要做更多的工作:

Dim found3 As String = My.Computer.FileSystem.GetDirectories("Y:\Sample Code Sequence\", _
                                      FileIO.SearchOption.SearchAllSubDirectories, _
                                      "*1089").Where(Function(x)
                                                         Return x.LastIndexOf("\"c) = x.LastIndexOf("1089") - 5 _
                                                             AndAlso IsNumeric(x.Substring(x.LastIndexOf("\"c) + 1, 4))
                                                     End Function).FirstOrDefault()
MessageBox.Show(found3)
如果您首先找到文件夹,然后单独检查文件夹名称,而不是像我所做的那样作为一条语句,则更容易理解


您还应该检查是否不匹配,
Nothing

您可以使用接受通配符的
My.Computer.FileSystem.GetDirectories
():

Dim found As String = My.Computer.FileSystem.GetDirectories("Y:\Sample Code Sequence\", _
                                      FileIO.SearchOption.SearchAllSubDirectories, _
                                      "*1089").FirstOrDefault()
MessageBox.Show(found)
要应用此方法,需要将要查找的值连接到“*”

您可以添加进一步的检查;例如,要检查文件夹名称是否有8个字符(以1089结尾),请执行以下操作:

这实际上可以通过使用
??1089
进行搜索来简化,其中?是单个字符的通配符

检查文件夹名称是否为8个字符(以4位数字开头),需要做更多的工作:

Dim found3 As String = My.Computer.FileSystem.GetDirectories("Y:\Sample Code Sequence\", _
                                      FileIO.SearchOption.SearchAllSubDirectories, _
                                      "*1089").Where(Function(x)
                                                         Return x.LastIndexOf("\"c) = x.LastIndexOf("1089") - 5 _
                                                             AndAlso IsNumeric(x.Substring(x.LastIndexOf("\"c) + 1, 4))
                                                     End Function).FirstOrDefault()
MessageBox.Show(found3)
如果您首先找到文件夹,然后单独检查文件夹名称,而不是像我所做的那样作为一条语句,则更容易理解


您还应该检查是否不匹配,
Nothing

感谢我所做的一切:私有子GoBut\u单击(发件人作为System.Object,e作为System.EventArgs)处理GoBut。单击Dim wdcode作为字符串wdcode=TextCode。Text Dim wd\u wild作为字符串wd\u wild=“*”Dim wd_full作为String wd_full=wd_wild&wdcode Dim作为String=My.Computer.FileSystem.GetDirectories(“Y:\Sample Code Sequence\”,FileIO.SearchOption.SearchAllSubDirectories,wd_full)。FirstOrDefault()进程。Start(“explorer.exe”,String.Format(“/n,/e,{0}”,find))结束子任务我所追求的是:私有子任务GoBut\u单击(发送者作为System.Object,e作为System.EventArgs)处理GoBut。单击Dim wdcode作为String wdcode=TextCode。Text Dim wd\u wild作为String wd\u wild=“*”Dim wd_full作为String wd_full=wd_wild&wdcode Dim作为String=My.Computer.FileSystem.GetDirectories(“Y:\Sample Code Sequence\”,FileIO.SearchOption.SearchAllSubDirectories,wd_full)。FirstOrDefault()进程。Start(“explorer.exe”,String.Format(“/n,/e,{0}”,find))端接头