C# 把一個檔案夾下所有檔案複製到另一個檔案夾下 把一個檔案夾下所有檔案刪除

來源:互聯網
上載者:User

標籤:foreach   title   cat   director   exce   dir   建立   create   static   

 
public static void CopyDirectory(string srcPath, string destPath){  try    {
    DirectoryInfo dir = new DirectoryInfo(srcPath);
    FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //擷取目錄下(不包含子目錄)的檔案和子目錄     foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判斷是否檔案夾 { if (!Directory.Exists(destPath+"\\"+i.Name)) { Directory.CreateDirectory(destPath + "\\" + i.Name); //目標目錄下不存在此檔案夾即建立子檔案夾 } CopyDir(i.FullName, destPath + "\\" + i.Name); //遞迴調用複製子檔案夾 } else { File.Copy(i.FullName, destPath + "\\" + i.Name,true); //不是檔案夾即複製檔案,true表示可以覆蓋同名檔案 } } } catch (Exception e) { throw; }
}

 

調用CopyDirectory方法前可以先判斷原路徑與目標路徑是否存在


if(Directory.Exists(srcPath)&&Directory.Exists(destPath)){ CopyDirectory(srcPath,destPath);
}

 原文地址:http://www.cnblogs.com/iamlucky/p/5996222.html

 

C# 把一個檔案夾下所有檔案刪除 
public static void DelectDir(string srcPath)
{ try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目錄中所有檔案和子目錄 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判斷是否檔案夾 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //刪除子目錄和檔案 } else { File.Delete(i.FullName); //刪除指定檔案 } } } catch (Exception e) { throw; }
}

 

調用DelectDir方法前可以先判斷檔案夾是否存在

if(Directory.Exists(srcPath)){    DelectDir(srcPath);}

 原文地址:http://www.cnblogs.com/iamlucky/p/5997865.html

C# 把一個檔案夾下所有檔案複製到另一個檔案夾下 把一個檔案夾下所有檔案刪除(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.