這段代碼的問題是:

我們提供的服務有:成都做網站、成都網站建設、微信公眾號開發、網站優化、網站認證、太平ssl等。為近1000家企事業單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的太平網站制作公司
首先for each循環在files里查找文件對象,但是f變量聲明為string了,無法讓它獲取對象;
然后file.copy的變量file沒有任何聲明,也沒有初始化賦值等;
最后,整個for each循環里查找集合files的文件,由于初始化的循環計數器仍然按照最初設定的文件數目向后遞增(文件地址),但是實際文件刪除一部分,后續的文件位置就不能跟循環計數指針匹配了,所以默認為查找不到文件,將會報錯。一般的做法時把復制和刪除分別放在2次循環里,第一次只復制文件,第二次才去刪除。
Private Sub btnRemovePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemovePath.Click
Try
' 先建立目錄以便用于后續的刪除示范。
If Not Directory.Exists("D:\網易") Then
Directory.CreateDirectory(" D:\網易 \Test1")
Directory.CreateDirectory(" D:\網易 \Test2")
Directory.CreateDirectory(" D:\網易 \Test3")
End If
' 刪除子目錄 Test1。
Directory.Delete(" D:\網易 \Test1", True)
' 刪除子目錄 Test2。
Dim myDirectoryInfo As New DirectoryInfo(" D:\網易 \Test2")
myDirectoryInfo.Delete(True)
' 將目錄 C:\AlexDirDemo 及其以下的文件和子目錄全數刪除。
Directory.Delete(" D:\網易 ", True)
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
' 啟動 Windows 資源管理器。
Process.Start("explorer.exe", "D:\")
End Sub
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控件。
Private?Sub?btnRemovePath_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?btnRemovePath.Click
Try
'?先建立目錄以便用于后續的刪除示范。
If?Not?Directory.Exists("D:\網易")?Then
Directory.CreateDirectory("?D:\網易?\Test1")
Directory.CreateDirectory("?D:\網易?\Test2")
Directory.CreateDirectory("?D:\網易?\Test3")
End?If
'?刪除子目錄?Test1。
Directory.Delete("?D:\網易?\Test1",?True)
'?刪除子目錄?Test2。
Dim?myDirectoryInfo?As?New?DirectoryInfo("?D:\網易?\Test2")
myDirectoryInfo.Delete(True)
'?將目錄?C:\AlexDirDemo?及其以下的文件和子目錄全數刪除。
Directory.Delete("?D:\網易?",?True)
Catch?ex?As?Exception
MessageBox.Show(ex.Message)
Exit?Sub
End?Try
'?啟動?Windows?資源管理器。
Process.Start("explorer.exe",?"D:\")
End?Sub
看看這個,有刪除的
Public strPath As String '要導出的文件夾路徑
Public NewFile As String '文件保存用
'創建文件夾
Public Sub CreatemyFolder(str As String)
Dim Mybook As Workbook
Dim f
Dim mypath As String
'Dim NewFile As String
Dim strPathFolder$
Dim abc As Object
NewFile = str
'強制覆蓋保存時,不讓確認框彈出?
Application.DisplayAlerts = False
Set Mybook = ThisWorkbook '把當前工作簿定義為變量Mybook
mypath = ThisWorkbook.Path "\"
strPathFolder = mypath NewFile
strPath = strPathFolder "\"
On Error Resume Next
Set abc = CreateObject("Scripting.FileSystemObject")
If abc.FolderExists(strPathFolder) = True Then
'===刪除文件夾==========
Set f = abc.GetFolder(strPathFolder)
f.Delete
abc.CreateFolder (strPathFolder)
'===刪除文件夾==========
Exit Sub
Else
abc.CreateFolder (strPathFolder)
End If
Set abc = Nothing
End Sub
本文名稱:vbnet刪除文件夾 vb刪除文件夾及文件
瀏覽路徑:http://www.yijiale78.com/article46/dodshhg.html
成都網站建設公司_創新互聯,為您提供定制網站、網頁設計公司、網站導航、服務器托管、網站收錄、全網營銷推廣
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯