日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 文件搬运(从一个文件夹Copy至另一个文件夹)

發布時間:2024/9/20 C# 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 文件搬运(从一个文件夹Copy至另一个文件夹) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

時常我們會遇到文件的復制、上傳等問題。特別是自動化生產方面,需要對機臺拋出的檔案進行搬運、收集,然后對資料里的數據等進行分析,等等。

Winform下,列舉集中較常見的檔案的搬運。

1 private void MoveFile()2 {3 string Frompath = @"D:\Test\OutPut";4 string directoryPath = @"D:\report";5 try6 {7 string[] picList = Directory.GetFiles(Frompath, "*.jpg"); //圖片8 string[] txtList = Directory.GetFiles(Frompath, "*.txt"); //文本文件9 string[] pdfList = Directory.GetFiles(Frompath, "*.pdf"); //PDF文件 10 11 foreach (string f in picList) 12 { 13 //取得文件名. 14 string fName = f.Substring(Frompath.Length + 1); 15 File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName), true); 16 } 17 foreach (string f in txtList) 18 { 19 string fName = f.Substring(Frompath.Length + 1); 20 try 21 { 22 File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName)); 23 } 24 // 捕捉異常. 25 catch (IOException copyError) 26 { 27 MessageBox.Show(copyError.Message); 28 } 29 } 30 foreach (string f in pdfList) 31 { 32 string fName = f.Substring(Frompath.Length + 1); 33 try 34 { 35 File.Copy(System.IO.Path.Combine(Frompath, fName), System.IO.Path.Combine(directoryPath, fName)); 36 } 37 catch (IOException copyError) 38 { 39 MessageBox.Show(copyError.Message); 40 return; 41 } 42 } 43 //刪除原始文件夾里的文件 44 foreach (string f in txtList) 45 { 46 File.Delete(f); 47 } 48 foreach (string f in picList) 49 { 50 File.Delete(f); 51 } 52 foreach (string f in pdfList) 53 { 54 File.Delete(f); 55 } 56 57 } 58 catch (DirectoryNotFoundException dirNotFound) 59 { 60 MessageBox.Show(dirNotFound.Message); 61 } 62 }

總結

以上是生活随笔為你收集整理的C# 文件搬运(从一个文件夹Copy至另一个文件夹)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。