从路径中获取文件夹名称

时间:2017-09-27 18:20:24

标签: vb.net path directory

有一个名称为temp的目录。它包含不同名称的各种文件夹。我想删除特定名称的文件夹,如test。如何删除vb.net中的内容。请帮帮我。

2 个答案:

答案 0 :(得分:0)

确保Imports System.IO

然后你可以这样做

File.Delete(path)

其中path是一个等于路径值的字符串。

答案 1 :(得分:0)

请注意以下代码并根据需要编辑代码:

Imports System.IO

Public Class Form1
  Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    Dim directoryName As String = "D:\_working"
    Dim subPath = directoryName & "\TEST"  '// be careful - subPath will be deleted!

    Try
      Dim directoryExists = Directory.Exists("D:\_working")
      Dim subDirectoryExists = Directory.Exists(subPath)

      MessageBox.Show("top-level directory exists: " & directoryExists)
      MessageBox.Show("sub-directory exists: " & subDirectoryExists)

      For Each deleteFile In Directory.GetFiles(subPath, "*.BMP", SearchOption.TopDirectoryOnly)
        File.Delete(deleteFile)
        '// you may want to log all deleted files here ...
      Next

      Directory.Delete(subPath) '// without the need of logging add ..(subPath, true) 

    Catch ex As Exception
      MessageBox.Show("The process failed: {0}", ex.Message)
    End Try

  End Sub
End Class