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# 文件操作的资料请关注我们其它相关文章!

(0)

相关推荐

  • C#实现的Excel文件操作类实例

    本文实例讲述了C#实现的Excel文件操作类.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Data.OleDb; using System.Text; using System.IO; namespace Hxh.API { /// <summary> /// ExcelOpration 的摘要说明. /// </summary> public class ExcelOpration { OleDbC

  • C#配置文件操作类分享

    C#配置文件操作类,供大家参考,具体内容如下 注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; using System.Configuration; namespace DotNet.Utilities.配置文件操作类 { public class ConfigHelper_sufei { /// <summary> /// 根据Key取Value值

  • C#文件操作类分享

    本文实例为大家分享了C#文件操作类的具体代码,供大家参考,具体内容如下 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Reflection; using System.Collections; using System.Data.Common; namespace DotNet.Utilities { //JSON转换类 public class

  • c# 配置文件App.config操作类库的方法

    实例如下: public class ConfigOperator { #region 从配置文件获取Value /// <summary> /// 从配置文件获取Value /// </summary> /// <param name="key">配置文件中key字符串</param> /// <returns></returns> public static string GetValueFromConfig(

  • C#实现的基于二进制读写文件操作示例

    本文实例讲述了C#实现的基于二进制读写文件操作.分享给大家供大家参考,具体如下: using System; using System.IO; class MyStream { private const string FILE_NAME = "Test.data"; public static void Main(String[] args) { // Create the new, empty data file. if (File.Exists(FILE_NAME)) { Con

  • 浅析C#中文件路径的操作

    在程序中对文件操作是非常常见的,而对文件的操作则不可避免的需要文件的路径,并对文件的路径进行一系列的操作,例如:判断已知的路径是一个目录还是一个文件,路劲是一个文件则该文件的名称是什么,文件的扩展名名是什么等等.在C#中并并没有将文件的路径抽象为一个类,用来表示文件路径就是一个普通的字符串.对文件路径的操作例如,要获取文件名称,可以通过截取字符串或者使用正则表达式来取得. 其实,在.NET类库中,有一个专门的功能类System.IO.Path,对表示文件或在目录路径的string进行操作.下面介

  • C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    本文实例讲述了C#实现的文件操作封装类.分享给大家供大家参考,具体如下: 最近发现群共享里面有个C# 文件操作封装类,其方法是调用Windows API 来操作的文件的删除.移动.复制.重命名操作.下载下来一试,发现果然不错,特在此记录,以防丢失! 文件操作类代码如下: using System; using System.Runtime.InteropServices; using System.IO; namespace LxFile { /// <summary> /// 文件操作代理,

  • C#实现ini文件读写操作

    本文实例为大家分享了C#语言实现ini文件读写操作的具体代码,供大家参考,具体内容如下 1.ini文件是什么? 见百度百科 2.C#语言实现ini文件的读写操作 /// <summary> /// 配置文件 .ini操作类 /// </summary> public class IniFileUtils { /// <summary> /// 写入INI文件 /// </summary> /// <param name="section&qu

  • C#操作INI配置文件示例详解

    本文实例为大家分享了C#操作INI配置文件示例的具体代码,供大家参考,具体内容如下 源文件地址:C#操作INI配置文件示例 创建如图所示的控件: 源代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Win

  • C#操作INI文件的辅助类IniHelper

    使用INI配置文件,简单便捷. 该辅助工具类为C#操作INI文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,在此深感遗憾,并对贡献者表示感谢. using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace Eyuan.Common { pub

  • C#创建及读取DAT文件操作

    *.DAT 从后缀名上也能理解其中的含义即:data文件,数据文件:这个文件有的可以用记事本工具打开,但是加密后就不一定了. 很多程序都创建dat文件来保存设定.创建一个只有自己才能解析得dat文件,并且可以读取和写入,写一个类,它可以创建一个只有使用这个类才能解析得dat文件. 同时读取或写入数据进dat文件时,也只可以使用这个类来读取写入. 简单来说就是这个类是打开读取写入这个dat文件得钥匙!其实用来保存设置的文件后缀名可以各种各样. 一.读写设置文件类 /// <summary> //

  • FtpHelper实现ftp服务器文件读写操作(C#)

    最近做了一个项目,需要读取ftp服务器上的文件,于是参考了网上提供的一些帮组方法,使用过程中,出现一些小细节问题,于是本人做了一些修改,拿来分享一下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Threading; using System.Configuration; na

随机推荐