如何使用代码从目录中搜索和删除多个文本文件

时间:2022-03-30 16:51:39

I have a treeview control on my form and from this form, I save data to text files to a desktop directory in the format:

我在我的表单上有一个treeview控件,从这个表单中,我将数据保存到文本文件到桌面目录,格式如下:

  • Dan Michael.TXT
  • Dan Michael.GAT
  • Michael Torns.STD

I also have a button on the form which the user can click to delete any node, after that node has been selected.

我还在表单上有一个按钮,用户可以在选择该节点后单击以删除任何节点。

In my treeview, there is a node whose text is “Michael”.

在我的树视图中,有一个节点,其文本为“Michael”。

What I want to achieve is that after the user selects this node, and then clicks the delete button, the node is deleted, and all text files in the desktop directory that have “Michael” in their names are also deleted.

我想要实现的是,在用户选择此节点,然后单击删除按钮后,该节点将被删除,并且桌面目录中名称中包含“Michael”的所有文本文件也将被删除。

I am able to specify the sub-folder in which the text files are found but do not know how to proceed to achieve my goal so would appreciate some help, please.

我能够指定找到文本文件的子文件夹,但不知道如何继续实现我的目标,所以请欣赏一些帮助。

I am using Visual Basic 2010 Express.

我正在使用Visual Basic 2010 Express。

‘This is the sub-folder directory in which the text files are found
Dim GB = New DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Student Names\" & TextBox1.Text))

Dim FGY As TreeNode 
    FGY = TreeView1.SelectedNode

‘Need help here…

1 个解决方案

#1


Use the .GetFiles to list all files with specific search pattern :

使用.GetFiles列出具有特定搜索模式的所有文件:

Dim pattern As string = "*" & FGY.Text & "*"
    For Each fi In GB.GetFiles(pattern)
            fi.Delete
Next

#1


Use the .GetFiles to list all files with specific search pattern :

使用.GetFiles列出具有特定搜索模式的所有文件:

Dim pattern As string = "*" & FGY.Text & "*"
    For Each fi In GB.GetFiles(pattern)
            fi.Delete
Next