如何使搜索栏等于vb.net中的Windows文件浏览器?

时间:2019-06-07 14:56:26

标签: vb.net

我在vb.net中有一个文件浏览器,我放了一个TextBox和一个按钮,当我在TextBox中键入内容时,它会查找文档,图像或与我键入的名称相同的任何其他文件的名称或者,如果没有,则使用最相似的名称,或者在任何情况下都没有键入或相似名称的文件,完全与文件浏览器窗口的名称相同。 (如果您有TextBox的获取方式而无需按钮,那对我来说还是更好的选择) 我已经参与了这个项目,但是现在所缺少的只是现在。我已经有一个示例路径,当我启动文件浏览器时,它已经采用了我设置的启动路径,当它启动时,它已经打开了路径“ C:\ Program Files”,并且正是从该路径输入的在TextBox中找到一个名称,它会查找所有文档,图像或其他任何与我键入的名称相同的文件(如果有的话),并且还会在子文件夹中搜索是否有任何带有我键入名称的文件。

以下示例中的图像更详细地显示了我想要的内容。

Windows explorer demo pictures

Windows文件资源管理器演示gif:https://j.gifs.com/ROL4Xw.gif

The design of my file explorer.

无需显示该文件的路径,如下图所示:Image of the path to the file that does not need to be shown

我所有的项目代码:

    Imports System.IO

Public Class Form1
    Dim path As String
    Dim nextPath As String
    Public Property ListView1 As Object

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        On Error Resume Next
        path = TextBox1.Text
        If (My.Computer.FileSystem.DirectoryExists(path)) Then

            explorer.Clear()

            For Each i In My.Computer.FileSystem.GetDirectories(path)
                explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 2)

            Next
            For Each i In My.Computer.FileSystem.GetFiles(path)
                explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 1)
            Next
        Else
            MsgBox("Its A File")
            'or user
            'pocess.Start(path) // to open the file
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            path = "C:\Program Files"
            For Each i In My.Computer.FileSystem.GetDirectories(path)
                explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 2)

            Next
            For Each i In My.Computer.FileSystem.GetFiles(path)
                explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 1)
            Next
        Catch ex As Exception
            MsgBox("...")
            Application.Exit()
        End Try

    End Sub

    Private Sub ListView1_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles explorer.ItemSelectionChanged
        nextPath = path + "\" + e.Item.Text

    End Sub

    Private Sub ListView1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles explorer.MouseDoubleClick

        Try

            If (My.Computer.FileSystem.DirectoryExists(nextPath)) Then
                path = nextPath
                explorer.Clear()
                TextBox1.Text = path
                For Each i In My.Computer.FileSystem.GetDirectories(path)
                    explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 2)

                Next
                For Each i In My.Computer.FileSystem.GetFiles(path)
                    explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 1)
                Next
            Else

                Process.Start(path & "\" & explorer.SelectedItems(0).Text)
            End If

        Catch ex As Exception
            MsgBox("...")
        End Try

    End Sub

    Private Sub BtnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click

        Try
            nextPath = path.Substring(0, path.LastIndexOf("\"))
            path = nextPath
            explorer.Clear()
            TextBox1.Text = path
            For Each i In My.Computer.FileSystem.GetDirectories(path)
                explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 2)

            Next
            For Each i In My.Computer.FileSystem.GetFiles(path)
                explorer.Items.Add(i.Substring(i.LastIndexOf("\") + 1), ImageList1.Images.Count() - 1)
            Next
        Catch ex As Exception
            MsgBox("...")
            Application.Restart()


        End Try

    End Sub



    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub
End Class

如果您要测试计算机以最佳解决此问题,我将发送整个项目:https://1drv.ms/u/s!ArgT_dApPP30jZMh6yyaBtIck40PFg?e=afhOng

在我发送的图像中,红色TextBox2和下面的代码中声明的button2被标记。

在我发送的图像“ The design of my file explorer”中,红色,TextBox2和button2在下面的代码中声明。

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub

请记住,如果您有一种只使用TextBox进行搜索而不需要按钮的方法,那对我来说是最好的。

1 个答案:

答案 0 :(得分:1)

使用IExplorerBrowser + ISearchFolderItemFactory

结果将与资源管理器相同。

使用P / Invoke在VB.NET中进行测试:

enter image description here