c# 文件操作(移动,复制,重命名)

 更新时间:2020年12月21日 09:27:29   作者:yangyang  
这篇文章主要介绍了c# 如何对文件操作(移动,复制,重命名),帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下

文件移动

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
      {
    

文件复制

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);
      }
    }

文件重命名

public ExecutionResult FileRename(string sourceFile, string destinationPath, string destinationFileName)
    {
      ExecutionResult result;
      FileInfo tempFileInfo;
      FileInfo tempBakFileInfo;
      DirectoryInfo tempDirectoryInfo;

      result = new ExecutionResult();
      tempFileInfo = new FileInfo(sourceFile);
      tempDirectoryInfo = new DirectoryInfo(destinationPath);
      tempBakFileInfo = new FileInfo(destinationPath + "\\" + destinationFileName);
      try
      {
        if (!tempDirectoryInfo.Exists)
          tempDirectoryInfo.Create();
        if (tempBakFileInfo.Exists)
          tempBakFileInfo.Delete();
        //move file to bak
        tempFileInfo.MoveTo(destinationPath + "\\" + destinationFileName);

        result.Status = true;
        result.Message = "Rename file OK";
        result.Anything = "OK";
      }
      catch (Exception ex)
      {
        result.Status = false;
        result.Anything = "Mail";
        result.Message = ex.Message;
        if (mesLog.IsErrorEnabled)
        {
          mesLog.Error(MethodBase.GetCurrentMethod().Name, "Rename file error. Msg :" + ex.Message);
          mesLog.Error(ex.StackTrace);
        }
      }

      return result;
    }

以上就是c# 文件操作(移动,复制,重命名)的详细内容,更多关于c# 文件操作的资料请关注脚本之家其它相关文章!

相关文章

  • 深入分析C# Task

    深入分析C# Task

    这篇文章主要介绍了C# Task的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习C# Task的相关知识,感兴趣的朋友可以了解下
    2020-08-08
  • C#中判断、验证字符串是否为日期格式的实现代码

    C#中判断、验证字符串是否为日期格式的实现代码

    这篇文章主要介绍了C#中判断、验证字符串是否为日期格式的实现代码,使用DateTime类中自带的两个方法实现,需要的朋友可以参考下
    2014-08-08
  • 浅析C#封装GRPC类库及调用简单实例

    浅析C#封装GRPC类库及调用简单实例

    这篇文章主要为大家详细介绍了C#中封装GRPC类库及调用简单实例的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-04-04
  • C# WinForm捕获未处理的异常实例解析

    C# WinForm捕获未处理的异常实例解析

    这篇文章主要介绍了C# WinForm捕获未处理的异常,包括了常见的未捕获的异常、UI线程异常、非UI线程异常等,非常实用,需要的朋友可以参考下
    2014-09-09
  • WinForm中comboBox控件数据绑定实现方法

    WinForm中comboBox控件数据绑定实现方法

    这篇文章主要介绍了WinForm中comboBox控件数据绑定实现方法,结合实例形式分析了WinForm实现comboBox控件数据绑定的常用方法与相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • WinForm中实现picturebox自适应图片大小的方法

    WinForm中实现picturebox自适应图片大小的方法

    这篇文章主要介绍了WinForm中实现picturebox自适应图片大小的方法,涉及pictureBox控件相关属性设置技巧,需要的朋友可以参考下
    2017-05-05
  • WinForm中Application.Idle方法详解

    WinForm中Application.Idle方法详解

    本文详细讲解了WinForm中的Application.Idle方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#实现文字转语音功能

    C#实现文字转语音功能

    这篇文章主要为大家详细介绍了C#实现文字转语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • C#中隐式运行CMD命令行窗口的方法

    C#中隐式运行CMD命令行窗口的方法

    下面介绍一种常用的在C#程序中调用CMD.exe程序,并且不显示命令行窗口界面,来完成CMD中各种功能的简单方法。
    2011-04-04
  • Unity读取Excel文件转换XML格式文件

    Unity读取Excel文件转换XML格式文件

    这篇文章主要为大家详细介绍了Unity读取Excel文件转换XML格式文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06

最新评论