using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Windows.Forms; using System.Threading; namespace BaseLibRWFile { public class FileOperate { #region folder & file Operate /// /// 判断文件夹是否存在 /// /// 文件路径 /// public static bool IsFolderExist(string folderPath) { DirectoryInfo folder = new DirectoryInfo(folderPath); if (folder.Exists) return true; return false; } /// /// 删除指定文件夹(包括里面的文件) /// /// 指定文件夹路径 /// public static bool DeleteFolder(string folderPath) { try { DirectoryInfo dir = new DirectoryInfo(folderPath); if (dir.Exists) { DirectoryInfo[] childs = dir.GetDirectories();//返回当前目录的子目录 foreach (DirectoryInfo child in childs) { child.Delete(true);//删除子文件 } dir.Delete(true);//删除文件夹 } return true; } catch { return false; } } /// /// 创建文件夹 /// /// public static void Directory_Create(string DirectoryFileName) { if (!Directory.Exists(DirectoryFileName)) { Directory.CreateDirectory(DirectoryFileName); } } /// /// 创建一个新的文件 /// /// /// public static bool CreateNewFile(string strPath) { try { if (!File.Exists(strPath)) { using (FileStream fs = new FileStream(strPath, FileMode.CreateNew)) fs.Close(); } //if (!(File.Exists(strPath))) //{ // File.Create(strPath).Close(); //} return true; } catch { return false; } } /// /// 根据路径创建路径的所有文件夹 /// /// /// public static bool GreatNewFolder(string folderPath) { try { string[] pathArray; string allPath = string.Empty; pathArray = folderPath.Split('\\'); allPath = pathArray[0]; for (int i = 0; i < pathArray.Length - 1; i++) { allPath = allPath + "\\" + pathArray[i + 1]; if (Directory.Exists(allPath) == false && pathArray[i + 1].IndexOf(".") == -1) { Directory.CreateDirectory(allPath); } } return true; } catch { return false; } } /// /// 判断文件是否存在 /// /// 文件路径 /// public static bool IsFileExist(string path) { return File.Exists(path); } /// /// 删除指定文件 /// /// 文件路径 /// public static bool DeleteFile(string path) { try { if (IsFileExist(path)) { File.Delete(path); return true; } else { return false; } } catch { return false; } } /// /// 复制指定文件到目标文件 /// /// 指定文件路径 /// 目标文件路径 /// public static bool CopyFile(string sourceFileName, string destFileName,bool isCoverage) { try { if (IsFileExist(sourceFileName)) { File.Copy(sourceFileName, destFileName,isCoverage); return true; } else { return false; } } catch { return false; } } /// /// 剪切指定文件到指定路径 可更改文件名 /// /// 指定文件路径 /// 目标文件路径 /// public static bool MoveFile(string sourceFileName, string destFileName) { try { if (IsFileExist(sourceFileName)) { File.Move(sourceFileName, destFileName); return true; } else { return false; } } catch { return false; } } /// /// 复制指定文件夹到指定路径 /// /// 指定文件夹路径 /// 目标路径 /// public static bool CopyFolder(string sourcePath, string destPath) { string path; string subFolderPath; try { DirectoryInfo folderPath = new DirectoryInfo(sourcePath); if (folderPath.Exists) { if (!Directory.Exists(destPath)) Directory.CreateDirectory(destPath); foreach (string sub in Directory.GetDirectories(sourcePath)) //获取指定目录中的子目录的名称(包括其路径) { subFolderPath = destPath + "\\" + Path.GetFileName(sub); CopyFolder(sub, subFolderPath); } foreach (string file in Directory.GetFiles(sourcePath)) //返回指定目录中文件的名称(包括其路径) { path = destPath + "\\" + Path.GetFileName(file);//返回指定路径字符串的文件名和扩展名 File.Copy(file, path, true); } return true; } else { return false; } } catch { return false; } } #endregion #region FileDelete public string str_DeletePath = string.Empty; public int i_HowLongFile = 72; public int[] i_DeleteTime; public string str_Type = string.Empty; public void FilesAllDelete(string strDeletePath, string strType, int iHowLongFile, int[] iDeleteTime) { str_DeletePath = strDeletePath; str_Type = strType; i_HowLongFile = iHowLongFile; i_DeleteTime = iDeleteTime; Thread DeleteFileThread = new Thread(new ThreadStart(DeleteFile)) { IsBackground = true }; DeleteFileThread.Start(); } public void DeleteFile() { int NowHour; bool IsDelete = false; while (true) { NowHour = DateTime.Now.Hour; if (i_DeleteTime.Any(a => a == NowHour) && !IsDelete) { StartDelete(str_DeletePath); IsDelete = true; } if (i_DeleteTime.Any(a => a != NowHour) && IsDelete) { IsDelete = false; } Thread.Sleep(1000); } } public bool StartDelete(string TPath) { try { ListFiles(new DirectoryInfo(TPath)); KillEmptyDirectory(TPath); return true; } catch (IOException ex) { MessageBox.Show($"删除路径{str_DeletePath}文件失败\r\nFile deletion failed\r\n{ex.Message}", "Warning"); return false; } } public void ListFiles(FileSystemInfo info) { if (!info.Exists) return; //不是目录 if (!(info is DirectoryInfo dir)) return; FileSystemInfo[] files = dir.GetFileSystemInfos(); DateTime nowtime = DateTime.Now; DateTime fileDate; TimeSpan cc; string fullpath; for (int i = 0; i < files.Length; i++) { //是文件 if (files[i] is FileInfo file) { fullpath = file.FullName; //fileDate=file.CreationTime; fileDate = file.LastWriteTime; cc = nowtime - fileDate; if (cc.Days * 24 + cc.Hours > i_HowLongFile && fullpath.Contains(str_Type)) { File.Delete(fullpath); } } else { ListFiles(files[i]); } } } public void KillEmptyDirectory(String storagepath) { DirectoryInfo dir = new DirectoryInfo(storagepath); DirectoryInfo[] subdirs = dir.GetDirectories("*.*", SearchOption.AllDirectories); foreach (DirectoryInfo subdir in subdirs) { FileSystemInfo[] subFiles = subdir.GetFileSystemInfos(); if (subFiles.Length == 0) { subdir.Delete(); } } } #endregion } }