文件操作类
Posted Carlos
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件操作类相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.IO; namespace FilesClass { public class FileOperate { /// <summary> /// 判断文件是否存在 /// </summary> /// <param name="path">文件路径</param> /// <returns>判断文件是否存在</returns> public static bool IsExistsFile(string path) { return File.Exists(path); } /// <summary> /// 判断文件夹是否存在 /// </summary> /// <param name="path">文件夹路径</param> /// <returns>判断文件夹是否存在</returns> public static bool IsExistsDir(string path) { return Directory.Exists(path); } /// <summary> /// /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool CreateDir(string path) { try { Directory.CreateDirectory(path); return true; } catch (Exception ex) { throw new Exception("创建文件夹失败,路径是>>" + path + " ,失败原因>>" + ex.Message); } } /// <summary> /// 删除文件 /// </summary> /// <param name="path">文件路径</param> public static void DeleteFile(string path) { if (IsExistsFile(path)) { File.Delete(path); } } /// <summary> /// 拷贝一个文件夹里面的东西 到另外一个文件夹 /// </summary> /// <param name="sourcePath"> 源文件夹 </param> /// <param name="targetPath"> 目标文件夹 </param> public static void CopyFolder(string sourcePath, string targetPath) { if (IsExistsDir(sourcePath)) { if (!IsExistsDir(targetPath)) { CreateDir(targetPath); } //获得源文件下所有文件 List<string> filesList = new List<string>(Directory.GetFiles(sourcePath)); filesList.ForEach(file => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(file)}); File.Copy(file, targetDir); }); //获取源文件下所有的目录文件 List<string> foldersList = new List<string>(Directory.GetDirectories(sourcePath)); foldersList.ForEach(folder => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(folder)}); CopyFolder(folder, targetDir); }); } else { throw new DirectoryNotFoundException("源目录不存在"); } } /// <summary> /// 移动文件夹到另外一个文件夹 /// </summary> /// <param name="sourcePath"> 源文件夹 </param> /// <param name="targetPath"> 目标文件夹 </param> public static void MoveFolder(string sourcePath, string targetPath) { if (IsExistsDir(sourcePath)) { if (IsExistsDir(targetPath)) { CreateDir(targetPath); } List<string> filesList = new List<string>(Directory.GetFiles(sourcePath)); filesList.ForEach(file => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(file)}); File.Copy(file, targetDir); }); List<string> folderList = new List<string>(Directory.GetDirectories(sourcePath)); folderList.ForEach(folder => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetDirectoryName(folder)}); MoveFolder(folder, targetDir); }); } else { throw new DirectoryNotFoundException("源目录不存在"); } } /// <summary> /// 删除文件夹 /// </summary> /// <param name="dirPath">目标文件夹</param> public static void DeleteFolder(string dirPath) { if (IsExistsDir(dirPath)) { foreach (string content in Directory.GetFileSystemEntries(dirPath)) { if (IsExistsDir(content)) { Directory.Delete(content, true); } else if (IsExistsFile(content)) { DeleteFile(content); } } } } /// <summary> /// 删除文件夹 删除文件夹然后新建 /// </summary> /// <param name="dirPath">目标文件夹</param> public static void DeleteFolderEx(string dirPath) { if (IsExistsDir(dirPath)) { Directory.Delete(dirPath, true); Directory.CreateDirectory(dirPath); } } /// <summary> /// 重命名文件夹 /// </summary> /// <param name="sourcePath"> 源文件夹 </param> /// <param name="targetPath"> 目标文件夹 </param> public static void RenameFolder(string sourcePath, string targetPath) { CopyFolder(sourcePath, targetPath); DeleteFolder(sourcePath); } } }
以上是关于文件操作类的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段15——git命令操作一个完整流程
VSCode自定义代码片段15——git命令操作一个完整流程
14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段