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