VB.NET文件夾操作相關(guān)技巧分享
作者:佚名
我們在這里為大家?guī)淼挠嘘P(guān)VB.NET文件夾操作的具體方法主要分為對文件夾的復(fù)制以及刪除這兩種,希望能對又需要的朋友有所幫助。
編程人員利用VB.NET進(jìn)行實際編程,可以輕松簡便的完成各種功能。比如今天為大家介紹的VB.NET在對文件夾的操作上,就可以輕松的實現(xiàn)各種操作。下面就讓我們一起來看看VB.NET文件夾操作相關(guān)應(yīng)用技巧。
VB.NET文件夾操作之文件夾復(fù)制
- Function CopyDir()Function CopyDir
(ByVal sourcePath As String, ByVal
targetPath As String) As Boolean- Try
- '檢查目標(biāo)目錄是否以目錄分割字符結(jié)束,
不是則添加- If Right(targetPath, 1) <> ""
Then targetPath += ""- '判斷目標(biāo)目錄是否存在,不存在則新建
- If Not Directory.Exists(targetPath)
Then Directory.CreateDirectory
(targetPath)- ' 得到源目錄的文件列表,該里面是包含
文件以及目錄路徑的一個數(shù)組- Dim fileList As String() =
Directory.GetFileSystemEntries
(sourcePath)- '遍歷所有的文件和目錄
- For Each filepath As String In
fileList- '目錄處理,遞歸
- If (Directory.Exists(filepath)) Then
- CopyDir(filepath, targetPath +
Path.GetFileName(filepath))- Else
VB.NET文件夾操作之復(fù)制文件
- File.Copy(filepath,
targetPath
+ Path.GetFileName
(filepath), True)- End If
- Next
- Return True
- Catch ex As Exception
- Return False
- End Try
- End Function
VB.NET文件夾操作之文件夾刪除
- Function DelDir()Function DelDir
(ByVal targetPath As String)
As Boolean- Try
- '檢查目標(biāo)目錄是否以目錄分割字符結(jié)束,
不是則添加- If Right(targetPath, 1) <> "
" Then targetPath += ""- '得到源目錄的文件列表,該里面是包
含文件以及目錄路徑的一個數(shù)組- Dim fileList As String() =
Directory.GetFileSystemEntries
(targetPath)- '遍歷所有的文件和目錄
- For Each filepath As String
In fileList- '目錄處理,遞歸
- If (Directory.Exists(filepath)) Then
- DelDir(targetPath + Path.GetFile
Name(filepath))- Else
- '刪除文件
- File.Delete(targetPath + Path.
GetFileName(filepath))- End If
- Next
- '刪除文件夾
- System.IO.Directory.Delete
(targetPath, True)- Return True
- Catch ex As Exception
- Return False
- End Try
- End Function
VB.NET文件夾操作的相關(guān)應(yīng)用方法就為大家介紹到這里。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
CSDN