123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- 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
- /// <summary>
- /// 判断文件夹是否存在
- /// </summary>
- /// <param name="folderPath">文件路径</param>
- /// <returns></returns>
- public static bool IsFolderExist(string folderPath)
- {
- DirectoryInfo folder = new DirectoryInfo(folderPath);
- if (folder.Exists)
- return true;
- return false;
- }
- /// <summary>
- /// 删除指定文件夹(包括里面的文件)
- /// </summary>
- /// <param name="folderPath">指定文件夹路径</param>
- /// <returns></returns>
- 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;
- }
-
- }
- /// <summary>
- /// 创建文件夹
- /// </summary>
- /// <param name="DirectoryFileName"></param>
- public static void Directory_Create(string DirectoryFileName)
- {
- if (!Directory.Exists(DirectoryFileName))
- {
- Directory.CreateDirectory(DirectoryFileName);
- }
- }
- /// <summary>
- /// 创建一个新的文件
- /// </summary>
- /// <param name="strPath"></param>
- /// <returns></returns>
- 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; }
- }
- /// <summary>
- /// 根据路径创建路径的所有文件夹
- /// </summary>
- /// <param name="folderPath"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 判断文件是否存在
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns></returns>
- public static bool IsFileExist(string path)
- {
- return File.Exists(path);
- }
- /// <summary>
- /// 删除指定文件
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns></returns>
- public static bool DeleteFile(string path)
- {
- try
- {
- if (IsFileExist(path))
- {
- File.Delete(path);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 复制指定文件到目标文件
- /// </summary>
- /// <param name="sourcePath">指定文件路径</param>
- /// <param name="destinationPath">目标文件路径</param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 剪切指定文件到指定路径 可更改文件名
- /// </summary>
- /// <param name="sourcePath">指定文件路径</param>
- /// <param name="destinationPath">目标文件路径</param>
- /// <returns></returns>
- public static bool MoveFile(string sourceFileName, string destFileName)
- {
- try
- {
- if (IsFileExist(sourceFileName))
- {
- File.Move(sourceFileName, destFileName);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 复制指定文件夹到指定路径
- /// </summary>
- /// <param name="sourcePath">指定文件夹路径</param>
- /// <param name="destPath">目标路径</param>
- /// <returns></returns>
- 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
- }
- }
|