VB如何删除指定目录下的文件夹,并且删除里面的子目录和文件并不会提示和删除到回收站
比如说D:\ProgramFiles\360在360里面有个123的文件夹建立一个command1command2和text1.command1是选择目录。在text1里...
比如说 D:\Program Files\360 在360里面有个123的文件夹
建立一个command1 command2 和 text1 .
command1是选择目录 。在 text1 里面显示 如 D:\Program Files\360
command2是 确定删除 点击后 删除 D:\Program Files\360 目录下的123文件夹,并且删除里面的子目录和文件并不会提示和删除到回收站
请高手指定。谢谢了。 展开
建立一个command1 command2 和 text1 .
command1是选择目录 。在 text1 里面显示 如 D:\Program Files\360
command2是 确定删除 点击后 删除 D:\Program Files\360 目录下的123文件夹,并且删除里面的子目录和文件并不会提示和删除到回收站
请高手指定。谢谢了。 展开
2013-04-27
展开全部
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控件。
更多追问追答
追问
我的是 VB6.0 能帮我详细下吗 我是 新手
追答
'首先添加一个模块(Module),里面是如下代码:
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const BIF_DONTGOBELOWDOMAIN = 2
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (LpBrowseInfo As BROWSEINFO) As Long
Public Declare Function SHGetPathFromIDlist Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public Type BROWSEINFO
hOwner As Long
pidlroot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lparam As Long
iImage As Long
End Type
Public Function GetFolder(ByVal hWnd As Long, Optional Title As String) As String
Dim bi As BROWSEINFO
Dim pidl As Long
Dim folder As String
folder = Space(255)
With bi
If IsNumeric(hWnd) Then .hOwner = hWnd
.ulFlags = BIF_RETURNONLYFSDIRS
.pidlroot = 0
If Title <> "" Then
.lpszTitle = Title & Chr$(0)
Else
.lpszTitle = "选择目录" & Chr$(0)
End If
End With
pidl = SHBrowseForFolder(bi)
If SHGetPathFromIDlist(ByVal pidl, ByVal folder) Then
GetFolder = Left(folder, InStr(folder, Chr$(0)) - 1)
Else
GetFolder = ""
End If
End Function
'下面是主窗体的代码
Private Sub Command1_Click()
Dim str
str = GetFolder(Me.hWnd, "浏览文件夹")
If str <> "" Then
Text1.Text = str
End If
End Sub
Private Sub Command2_Click()
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Text1.Text <> "" Then
objFSO.DeleteFolder (Text1.Text)
MsgBox "Finished!"
End If
End Sub
VB6.0的麻烦一些,代码如上。
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command2_Click()
CreateObject("Scripting.FileSystemObject").Getfolder(Text1).Delete True
End Sub
CreateObject("Scripting.FileSystemObject").Getfolder(Text1).Delete True
End Sub
追问
详细下可以么
追答
这个是删除目录的代码
如果要选择目录另有代码,不过好像你问的不是选择目录,而是删除目录
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |