c# 文件操作(移動(dòng),復(fù)制,重命名)
文件移動(dòng)
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 {
文件復(fù)制
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); } }
文件重命名
public ExecutionResult FileRename(string sourceFile, string destinationPath, string destinationFileName) { ExecutionResult result; FileInfo tempFileInfo; FileInfo tempBakFileInfo; DirectoryInfo tempDirectoryInfo; result = new ExecutionResult(); tempFileInfo = new FileInfo(sourceFile); tempDirectoryInfo = new DirectoryInfo(destinationPath); tempBakFileInfo = new FileInfo(destinationPath + "\\" + destinationFileName); try { if (!tempDirectoryInfo.Exists) tempDirectoryInfo.Create(); if (tempBakFileInfo.Exists) tempBakFileInfo.Delete(); //move file to bak tempFileInfo.MoveTo(destinationPath + "\\" + destinationFileName); result.Status = true; result.Message = "Rename file OK"; result.Anything = "OK"; } catch (Exception ex) { result.Status = false; result.Anything = "Mail"; result.Message = ex.Message; if (mesLog.IsErrorEnabled) { mesLog.Error(MethodBase.GetCurrentMethod().Name, "Rename file error. Msg :" + ex.Message); mesLog.Error(ex.StackTrace); } } return result; }
以上就是c# 文件操作(移動(dòng),復(fù)制,重命名)的詳細(xì)內(nèi)容,更多關(guān)于c# 文件操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 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ù)制
- C#實(shí)現(xiàn)文件Move和Copy操作
相關(guān)文章
C# XML基礎(chǔ)入門(mén)小結(jié)(XML文件內(nèi)容增刪改查清)
本文主要介紹了C# XML基礎(chǔ)入門(mén)小結(jié)(XML文件內(nèi)容增刪改查清),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼
在微信小程序管理后臺(tái),我們可以生成下載標(biāo)準(zhǔn)的小程序二維碼,提供主程序入口功能,在實(shí)際應(yīng)用開(kāi)發(fā)中,小程序二維碼是可以攜帶參數(shù)的,可以動(dòng)態(tài)進(jìn)行生成,本文小編就給大家介紹一下如何基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼,感興趣的朋友可以參考下2023-12-12C#使用ZBar實(shí)現(xiàn)識(shí)別條形碼
目前主流的識(shí)別庫(kù)主要有ZXing.NET和ZBar,本文主要介紹的是如何使用ZBar庫(kù)實(shí)現(xiàn)識(shí)別條形碼功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07c# socket網(wǎng)絡(luò)編程接收發(fā)送數(shù)據(jù)示例代碼
這篇文章主要介紹了c# socket網(wǎng)絡(luò)編程,server端接收,client端發(fā)送數(shù)據(jù),大家參考使用吧2013-12-12C#實(shí)現(xiàn)將RTF轉(zhuǎn)為HTML的示例代碼
RTF文檔即富文本格式(Rich?Text?Format)的文檔。我們?cè)谔幚砦募r(shí),遇到需要對(duì)文檔格式進(jìn)行轉(zhuǎn)換時(shí),可以將RTF轉(zhuǎn)為其他格式,如轉(zhuǎn)為DOCX/DOC、PDF或者HTML。本文將利用C#實(shí)現(xiàn)RTF轉(zhuǎn)HTML,需要的可以參考一下2022-04-04C#連接Oracle數(shù)據(jù)庫(kù)的實(shí)例方法
C#連接Oracle數(shù)據(jù)庫(kù)的實(shí)例方法,需要的朋友可以參考一下2013-04-04C#實(shí)現(xiàn)給PDF文檔設(shè)置過(guò)期時(shí)間
我們可以給一些重要文檔或者臨時(shí)文件設(shè)置過(guò)期時(shí)間和過(guò)期信息提示來(lái)提醒讀者或管理者文檔的時(shí)效性,并及時(shí)對(duì)文檔進(jìn)行調(diào)整、更新等。下面本文將介紹如何通過(guò)C#來(lái)給PDF文檔設(shè)置過(guò)期時(shí)間的方法。需要的可以參考一下2022-01-01C# WinForm自動(dòng)更新程序之文件上傳操作詳解
這篇文章主要為大家詳細(xì)介紹了C# WinForm自動(dòng)更新程序中文件上傳操作,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2022-10-10C# .Net實(shí)現(xiàn)灰度圖和HeatMap熱力圖winform(進(jìn)階)
本文主要介紹了C# .NET實(shí)現(xiàn)簡(jiǎn)易灰度圖和酷炫HeatMap熱力圖winform,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12