zoukankan      html  css  js  c++  java
  • C# 文件搬运(从一个文件夹Copy至另一个文件夹)

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

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

    复制代码
     1      private void MoveFile()
     2         {
     3             string Frompath = @"D:TestOutPut";
     4             string directoryPath = @"D:
    eport";
     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         }
    复制代码
  • 相关阅读:
    chapter16 计算机体系结构基础
    ASP.NET 2.0加密Web.config 配置文件
    用IIS6.0的Kernel Caching 压缩技术提高应用程序性能
    .net中怎样执行一个字符串
    ASP.NET 页面生存周期中的关键事件
    ASP.NET获取客户端IP及MAC地址
    DOS命令关闭计算机
    1个式子检测密码强度
    徐州话六级考试
    .net2.0中新增的Substitution控件
  • 原文地址:https://www.cnblogs.com/soundcode/p/10862847.html
Copyright © 2011-2022 走看看