C#實(shí)現(xiàn)文件Move和Copy操作
文件移動(dòng)(Move)操作和文件的復(fù)制(Copy)是C#程式開(kāi)發(fā)經(jīng)常遇到的方法,根據(jù)傳入的源文件地址和目標(biāo)文件地址參數(shù),實(shí)現(xiàn)對(duì)文件的操作。實(shí)現(xiàn)代碼如下:
Move操作代碼
public static void MoveFolder(string sourcePath, string destPath) { if (Directory.Exists(sourcePath)) { if (!Directory.Exists(destPath)) { //目標(biāo)目錄不存在則創(chuàng)建 try { Directory.CreateDirectory(destPath); } catch (Exception ex) { throw new Exception("創(chuàng)建目標(biāo)目錄失?。? + ex.Message); } } //獲得源文件下所有文件 List<string> files = new List<string>(Directory.GetFiles(sourcePath)); files.ForEach(c => { string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) }); //覆蓋模式 if (File.Exists(destFile)) { File.Delete(destFile); } File.Move(c, destFile); }); //獲得源文件下所有目錄文件 List<string> folders = new List<string>(Directory.GetDirectories(sourcePath)); folders.ForEach(c => { string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) }); //Directory.Move必須要在同一個(gè)根目錄下移動(dòng)才有效,不能在不同卷中移動(dòng)。 //Directory.Move(c, destDir); //采用遞歸的方法實(shí)現(xiàn) MoveFolder(c, destDir); }); } else {} }
Copy操作代碼
public static void CopyFilefolder(string sourceFilePath, string targetFilePath) { //獲取源文件夾中的所有非目錄文件 string[] files = Directory.GetFiles(sourceFilePath); string fileName; string destFile; //如果目標(biāo)文件夾不存在,則新建目標(biāo)文件夾 if (!Directory.Exists(targetFilePath)) { Directory.CreateDirectory(targetFilePath); } //將獲取到的文件一個(gè)一個(gè)拷貝到目標(biāo)文件夾中 foreach (string s in files) { fileName = Path.GetFileName(s); destFile = Path.Combine(targetFilePath, fileName); File.Copy(s, destFile, true); } //上面一段在MSDN上可以看到源碼 //獲取并存儲(chǔ)源文件夾中的文件夾名 string[] filefolders = Directory.GetFiles(sourceFilePath); //創(chuàng)建Directoryinfo實(shí)例 DirectoryInfo dirinfo = new DirectoryInfo(sourceFilePath); //獲取得源文件夾下的所有子文件夾名 DirectoryInfo[] subFileFolder = dirinfo.GetDirectories(); for (int j = 0; j < subFileFolder.Length; j++) { //獲取所有子文件夾名 string subSourcePath = sourceFilePath + "\\" + subFileFolder[j].ToString(); string subTargetPath = targetFilePath + "\\" + subFileFolder[j].ToString(); //把得到的子文件夾當(dāng)成新的源文件夾,遞歸調(diào)用CopyFilefolder CopyFilefolder(subSourcePath, subTargetPath); } }
到此這篇關(guān)于C#實(shí)現(xiàn)文件Move和Copy操作的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- c# 文件操作(移動(dòng),復(fù)制,重命名)
- C#中使用FilleStream實(shí)現(xiàn)視頻文件的復(fù)制功能
- C# FileStream實(shí)現(xiàn)大文件復(fù)制
- C# FileStream復(fù)制大文件
- C#使用FileStream復(fù)制一個(gè)任意文件
- C#實(shí)現(xiàn)文件夾的復(fù)制和刪除
- C# 復(fù)制與刪除文件的實(shí)現(xiàn)方法
- C#程序中創(chuàng)建、復(fù)制、移動(dòng)、刪除文件或文件夾的示例
- C#將文件復(fù)制到指定文件夾并整理
- C#進(jìn)行文件讀寫(xiě)、創(chuàng)建、復(fù)制、移動(dòng)、刪除的方法
- C# 對(duì)文件與文件夾的操作包括刪除、移動(dòng)與復(fù)制
相關(guān)文章
C#通過(guò)鏈表實(shí)現(xiàn)隊(duì)列的方法
這篇文章主要介紹了C#通過(guò)鏈表實(shí)現(xiàn)隊(duì)列的方法,涉及C#操作鏈表的相關(guān)技巧,需要的朋友可以參考下2015-04-04用C#獲取硬盤(pán)序列號(hào),CPU序列號(hào),網(wǎng)卡MAC地址的源碼
用C#獲取硬盤(pán)序列號(hào),CPU序列號(hào),網(wǎng)卡MAC地址的源碼...2007-03-03基于Silverlight打印的使用詳解,是否為微軟的Bug問(wèn)題
本篇文章對(duì)Silverlight打印的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#使用密封類實(shí)現(xiàn)密封用戶信息的示例詳解
在C#中,密封類(sealed class)是一種不能被其他類繼承的類,它用于防止其他類繼承它的功能和屬性, 下面我們就來(lái)看看如何使用密封類密封用戶的信息吧2024-02-02C#實(shí)現(xiàn)六大設(shè)計(jì)原則之接口隔離原則
這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之接口隔離原則的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02