1. Private Sub CopyDir(ByVal srcPath As StringByVal aimPath As String
  2.  
  3.         Try 
  4.  
  5.             ' 检查目标目录是否以目录分割字符\结束,如果不是则添加之 
  6.  
  7.             If aimPath.Substring(aimPath.Length - 1) <> Path.DirectorySeparatorChar Then 
  8.  
  9.                 aimPath += Path.DirectorySeparatorChar 
  10.  
  11.             End If 
  12.  
  13.  
  14.             '判断源目录是否存在,不存在则退出. 
  15.  
  16.             If (Not Directory.Exists(srcPath)) Then Exit Sub 
  17.  
  18.  
  19.             '' 判断目标目录是否存在如果不存在则新建之 
  20.  
  21.             'If (Not Directory.Exists(aimPath)) Then Directory.CreateDirectory(aimPath) 
  22.  
  23.  
  24.             ' 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组 
  25.  
  26.             Dim fileList() As String = Directory.GetFileSystemEntries(srcPath) 
  27.  
  28.             ' 遍历所有的文件和目录 
  29.  
  30.  
  31.             For Each FileName As String In fileList 
  32.  
  33.                 ' 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 
  34.  
  35.                 If Directory.Exists(FileName) Then 
  36.  
  37.                     If (Not Directory.Exists(aimPath + Path.GetFileName(FileName))) Then Directory.CreateDirectory(aimPath + Path.GetFileName(FileName)) 
  38.  
  39.                     CopyDir(FileName, aimPath + Path.GetFileName(FileName)) 
  40.  
  41.                     ' 否则直接Copy文件 
  42.  
  43.                 Else 
  44.  
  45.                     File.Copy(FileName, aimPath + Path.GetFileName(FileName), True
  46.  
  47.                 End If 
  48.  
  49.             Next 
  50.  
  51.  
  52.         Catch ex As Exception 
  53.  
  54.             Response.Write("<br>" + ex.ToString()) 
  55.  
  56.         End Try 
  57.  
  58.     End Sub