栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 files = new List(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 folders = new List(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# 文件操作的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/121566.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号