VB如何删除指定目录下的文件夹,并且删除里面的子目录和文件并不会提示和删除到回收站

2024-11-17 11:52:55
推荐回答(4个)
回答(1):

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
Else
MsgBox("OpenFolder Error", MsgBoxStyle.Critical)
Exit Sub
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
If TextBox1.Text <> "" Then
My.Computer.FileSystem.DeleteDirectory(TextBox1.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
MessageBox.Show("Finished!")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class

在VB.NET 2010编译运行通过。

注意要加一个FolderBrowserDialog控件。

回答(2):

Sub RecurseTree(CurrPath As String) 'currpath问文件夹路径
Dim sFileName As String, newPath As String, sPath As String
Static oldPath As String
sPath = CurrPath & "\"
sFileName = Dir(sPath, 31) '31的含义∶31=vbNormal+vbReadOnly+vbHidden+vbSystem+vbVolume+vbDirectory
Do While sFileName <> ""
If sFileName <> "." And sFileName <> ".." Then
If GetAttr(sPath & sFileName) And vbDirectory Then '如果是目录和文件夹
newPath = sPath & sFileName
RecurseTree newPath
sFileName = Dir(sPath, 31)
Else
SetAttr sPath & sFileName, vbNormal
Kill (sPath & sFileName)
'Label1.Caption = sPath & sFileName '显示删除过程
sFileName = Dir
End If
Else
sFileName = Dir
End If
DoEvents
Loop
SetAttr CurrPath, vbNormal
RmDir CurrPath
'Label1.Caption = CurrPath
End Sub

回答(3):

Private Sub Command2_Click()
CreateObject("Scripting.FileSystemObject").Getfolder(Text1).Delete True
End Sub

回答(4):

看书吧,好好学习天天向上