教你更快速使用VB.NET文件夾操作
作者:佚名 
  本文主要就VB.NET文件夾操作進行代碼詳細(xì)演示,讓你更輕松的運用,代碼只要復(fù)雜粘貼到機器上就可以跑起來用,但是還是希望大家看懂下面的代碼。
 文件夾這個概念大家都很熟悉,在各各操作系統(tǒng)中都有文件夾這個概念,而在VB.NET這門開發(fā)語言中如何更好更安全的操作文件夾,這就是今天我們要來演示的一個案例。希望從VB.NET文件夾操作這個案例中學(xué)到技巧。
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
 - '復(fù)制文件
 - File.Copy(filepath, targetPath + Path.GetFileName(filepath), True)
 - End If
 - Next
 - Return True
 - Catch ex As Exception
 - Return False
 - End Try
 - End Function
 - '文件夾刪除
 - 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.GetFileName(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
 
以上就是我為大家提高的關(guān)于VB.NET文件夾操作的一個案例,大家快試試吧!
【編輯推薦】
責(zé)任編輯:田樹 
                    來源:
                    博客
 














 
 
 
 
 
 
 