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

时常我们会遇到文件的复制、上传等问题。特别是自动化生产方面,需要对机台抛出的档案进行搬运、收集,然后对资料里的数据等进行分析,等等。

Winform下,列举集中较常见的档案的搬运。

复制代码
 1      private void MoveFile()
 2         {
 3             string Frompath = @"D:\Test\OutPut";
 4             string directoryPath = @"D:\report";
 5             try
 6             {
 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         }
复制代码

 

转载于:https://www.cnblogs.com/soundcode/p/10862847.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中,你可以使用 `System.IO` 命名空间下的 `File` 类来复制文件。下面是一个简单的示例,演示如何从一个文件夹复制文件到另外一个文件夹: ```csharp using System.IO; class Program { static void Main(string[] args) { string sourceDirectory = @"C:\SourceFolder"; // 源文件夹路径 string targetDirectory = @"C:\TargetFolder"; // 目标文件夹路径 // 获取源文件夹中所有文件的路径 string[] files = Directory.GetFiles(sourceDirectory); // 遍历每个文件路径,并复制到目标文件夹 foreach (string file in files) { // 获取源文件文件名(包括扩展名) string fileName = Path.GetFileName(file); // 构建目标文件的完整路径 string targetPath = Path.Combine(targetDirectory, fileName); // 复制文件 File.Copy(file, targetPath, true); } Console.WriteLine("文件复制完成!"); } } ``` 在上述示例中,我们通过 `Directory.GetFiles()` 方法获取源文件夹中的所有文件路径。然后,使用 `Path.GetFileName()` 方法获取每个文件文件名(包括扩展名),并使用 `Path.Combine()` 方法构建目标文件的完整路径。最后,使用 `File.Copy()` 方法将源文件复制到目标文件夹中。第三个参数 `true` 表示如果目标文件夹中已存在同名文件,则覆盖它。 请确保你有足够的权限来读取源文件夹中的文件,并在目标文件夹中进行写入操作。另外,注意替换示例代码中的源文件夹和目标文件夹路径为你实际使用的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值