zoukankan      html  css  js  c++  java
  • C#实现文件Move操作和文件的Copy操作

    文件移动(Move)操作和文件的复制(Copy)是C#程式开发经常遇到的方法,根据传入的源文件地址和目标文件地址参数,实现对文件的操作。实现代码如下:

    1. Move操作代码:
      public static void MoveFolder(string sourcePath, string destPath)
              {
                  if (Directory.Exists(sourcePath))
                  {
                      if (!Directory.Exists(destPath))
                      {
                          //目标目录不存在则创建  
                          try
                          {
                              Directory.CreateDirectory(destPath);
                          }
                          catch (Exception ex)
                          {
                              throw new Exception("创建目标目录失败:" + 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必须要在同一个根目录下移动才有效,不能在不同卷中移动。  
                          //Directory.Move(c, destDir);  
      
                          //采用递归的方法实现  
                          MoveFolder(c, destDir);
                      });
                  }
                  else
                  {
             
      Move
    2. Copy操作代码:
      public static void CopyFilefolder(string sourceFilePath, string targetFilePath)
              {
                  //获取源文件夹中的所有非目录文件
                  string[] files = Directory.GetFiles(sourceFilePath);
                  string fileName;
                  string destFile;
                  //如果目标文件夹不存在,则新建目标文件夹
                  if (!Directory.Exists(targetFilePath))
                  {
                      Directory.CreateDirectory(targetFilePath);
                  }
                  //将获取到的文件一个一个拷贝到目标文件夹中  
                  foreach (string s in files)
                  {
                      fileName = Path.GetFileName(s);
                      destFile = Path.Combine(targetFilePath, fileName);
                      File.Copy(s, destFile, true);
                  }
                  //上面一段在MSDN上可以看到源码 
      
                  //获取并存储源文件夹中的文件夹名
                  string[] filefolders = Directory.GetFiles(sourceFilePath);
                  //创建Directoryinfo实例 
                  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();
                      //把得到的子文件夹当成新的源文件夹,递归调用CopyFilefolder
                      CopyFilefolder(subSourcePath, subTargetPath);
                  }
              }
      Copy
  • 相关阅读:
    知识图谱学习与实践(4)——通过例句介绍Sparql的使用
    知识图谱学习与实践(3)——知识表示
    知识图谱学习与实践(2)——知识图谱数据模型的构建
    知识图谱学习与实践(1)——知识图谱的演化过程
    NIO客户端主要创建过程
    NIO服务端主要创建过程
    Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bhive.session.id%7D_resources
    ubuntu中mysql安装失败
    使用ant build build.xml报“includeantruntime was not set”警告及"Class not found: javac1.8"问题
    maven编译报错 -source 1.5 中不支持 lambda(或diamond) 表达式,编码 UTF-8 的不可映射字符
  • 原文地址:https://www.cnblogs.com/wml-it/p/12148299.html
Copyright © 2011-2022 走看看