網站首頁 編程語言 正文
C#拷貝文件到另一個文件夾下
/// <summary>
/// 拷貝文件到另一個文件夾下
/// </summary>
/// <param name="sourceName">源文件路徑</param>
/// <param name="folderPath">目標路徑(目標文件夾)</param>
public void CopyToFile(string sourceName, string folderPath)
{
//例子:
//源文件路徑
//string sourceName = @"D:\Source\Test.txt";
//目標路徑:項目下的NewTest文件夾,(如果沒有就創建該文件夾)
//string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewTest");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
//當前文件如果不用新的文件名,那么就用原文件文件名
string fileName = Path.GetFileName(sourceName);
//這里可以給文件換個新名字,如下:
//string fileName = string.Format("{0}.{1}", "newFileText", "txt");
//目標整體路徑
string targetPath = Path.Combine(folderPath, fileName);
//Copy到新文件下
FileInfo file = new FileInfo(sourceName);
if (file.Exists)
{
//true 為覆蓋已存在的同名文件,false 為不覆蓋
file.CopyTo(targetPath, true);
}
}
注意方法內注釋的用法,可以根據自己的需要更改。
C#文件搬運(從一個文件夾Copy至另一個文件夾)
時常我們會遇到文件的復制、上傳等問題。特別是自動化生產方面,需要對機臺拋出的檔案進行搬運、收集,然后對資料里的數據等進行分析,等等。
Winform下,列舉集中較常見的檔案的搬運。
private void MoveFile()
{
string Frompath = @"D:\Test\OutPut";
string directoryPath = @"D:\report";
try
{
string[] picList = Directory.GetFiles(Frompath, "*.jpg"); //圖片
string[] txtList = Directory.GetFiles(Frompath, "*.txt"); //文本文件
string[] pdfList = Directory.GetFiles(Frompath, "*.pdf"); //PDF文件
foreach (string f in picList)
{
//取得文件名.
string fName = f.Substring(Frompath.Length + 1);
File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName), true);
}
foreach (string f in txtList)
{
string fName = f.Substring(Frompath.Length + 1);
try
{
File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName));
}
// 捕捉異常.
catch (IOException copyError)
{
MessageBox.Show(copyError.Message);
}
}
foreach (string f in pdfList)
{
string fName = f.Substring(Frompath.Length + 1);
try
{
File.Copy(System.IO.Path.Combine(Frompath, fName), System.IO.Path.Combine(directoryPath, fName));
}
catch (IOException copyError)
{
MessageBox.Show(copyError.Message);
return;
}
}
//刪除原始文件夾里的文件
foreach (string f in txtList)
{
File.Delete(f);
}
foreach (string f in picList)
{
File.Delete(f);
}
foreach (string f in pdfList)
{
File.Delete(f);
}
}
catch (DirectoryNotFoundException dirNotFound)
{
MessageBox.Show(dirNotFound.Message);
}
}
總結
原文鏈接:https://blog.csdn.net/qq_38693757/article/details/115730668
相關推薦
- 2022-08-02 利用Redis進行數據緩存的項目實踐_Redis
- 2022-05-10 @requestmapping獲取請求參數
- 2022-06-26 Docker上部署Nginx的方法步驟_docker
- 2023-02-09 利用C++開發一個protobuf動態解析工具_C 語言
- 2022-10-24 C語言控制進程之進程等待詳解_C 語言
- 2022-08-11 利用python繪制線型圖_python
- 2022-06-01 Python學習之內置函數總結_python
- 2022-02-09 QT5實現UDP通信的示例代碼_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支