服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - c# 文件操作(移动,复制,重命名)

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

2022-10-24 12:07yangyang C#

这篇文章主要介绍了c# 如何对文件操作(移动,复制,重命名),帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下

文件移动

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
      {
   

文件复制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
      }
    }

文件重命名

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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# 文件操作的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/wml-it/p/12148911.html

延伸 · 阅读

精彩推荐
  • C#Unity实现弧形移动效果

    Unity实现弧形移动效果

    这篇文章主要为大家详细介绍了Unity实现弧形移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    LLLLL__5562022-09-16
  • C#C#事件实例详解

    C#事件实例详解

    这篇文章主要介绍了C#事件实例详解的相关资料,需要的朋友可以参考下...

    13764947152022-01-10
  • C#使用c#+IMap实现收取163邮件

    使用c#+IMap实现收取163邮件

    本文给大家推荐的是使用c#+IMap实现收取163邮件的具体方法和示例代码,并附上了smtp邮箱发送邮件的代码,有需要的小伙伴可以参考下...

    鱼东东4392022-02-20
  • C#C#中委托(Delegates)的使用方法详解

    C#中委托(Delegates)的使用方法详解

    这篇文章主要为大家详细介绍了C#中委托(Delegates)的使用方法,感兴趣的朋友可以参考一下...

    C#教程网8662021-11-25
  • C#C#实现的文件批量重命名功能示例

    C#实现的文件批量重命名功能示例

    这篇文章主要介绍了C#实现的文件批量重命名功能,结合具体实例形式分析了C#针对文件的遍历、属性修改相关操作技巧,需要的朋友可以参考下...

    a7719485247622022-01-12
  • C#C#中Equals和GetHashCode使用及区别

    C#中Equals和GetHashCode使用及区别

    这篇文章主要介绍了C#中Equals和GetHashCode使用及区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    HueiFeng7022022-08-24
  • C#C#实现刷新桌面的方法

    C#实现刷新桌面的方法

    这篇文章主要介绍了C#实现刷新桌面的方法,涉及C#基于shell32.dll动态链接库实现系统桌面刷新的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    C#教程网3822021-10-26
  • C#C#中winform中panel重叠无法显示问题的解决

    C#中winform中panel重叠无法显示问题的解决

    这篇文章主要介绍了C#中winform中panel重叠无法显示问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    随风6666669422022-08-07