/* * 删除数据并无视异常 */ static public void DeleteOldFile() { try { DirectoryInfo floder = new DirectoryInfo(RunningEnv.ProgramParam.ToCheckTxtPaperPath); FileInfo[] fileInfo = floder.GetFiles(); foreach (FileInfo NextFile in fileInfo) { NextFile.Delete(); } floder = new DirectoryInfo(RunningEnv.ProgramParam.ReportPath); fileInfo = floder.GetFiles(); foreach (FileInfo NextFile in fileInfo) { NextFile.Delete(); } floder = new DirectoryInfo(RunningEnv.ProgramParam.ReportDataPath); fileInfo = floder.GetFiles(); foreach (FileInfo NextFile in fileInfo) { NextFile.Delete(); } } catch { } }
public void delete_check_data_file() { try { DirectoryInfo textFolder33 = new DirectoryInfo(to_check_txt_paper); DirectoryInfo textFolder11 = new DirectoryInfo(report); DirectoryInfo textFolder22 = new DirectoryInfo(report_data); FileInfo[] fileInfo = textFolder33.GetFiles(); foreach (FileInfo NextFile in fileInfo) { NextFile.Delete(); } fileInfo = textFolder11.GetFiles(); foreach (FileInfo NextFile in fileInfo) { NextFile.Delete(); } fileInfo = textFolder22.GetFiles(); foreach (FileInfo NextFile in fileInfo) { NextFile.Delete(); } } catch {} }
/// <summary> /// 清理全部过期图片 /// </summary> public ResultJson ClearAllTempImage() { var url = HttpContext.Current.Server.MapPath("/current/images/temp"); var ClearTime = MemCacheHelper2.Instance.Cache.GetModel <DateTime?>("ClearTime"); var flag = false; #region 查看是否到清理时间 if (ClearTime != null) { if (ClearTime.Value.Day != DateTime.Now.Day) { flag = true; MemCacheHelper2.Instance.Cache.Set("ClearTime", DateTime.Now); } } else { MemCacheHelper2.Instance.Cache.Set("ClearTime", DateTime.Now); flag = true; } #endregion if (flag) { #region 清除临时文件夹下超过3天的图片; DirectoryInfo TheFolder = new DirectoryInfo(url); foreach (FileInfo NextFile in TheFolder.GetFiles()) { if (DateTime.Now.Subtract(NextFile.CreationTime).TotalDays > 3) { NextFile.Delete(); } } #endregion #region 清除超过三天历史设计的图片(没有登入的用户) var Listhisdesign = HisdesignFunc.Instance.GetAllHisdesignInfo(); foreach (var item in Listhisdesign) { var deleteFlag = false; if (item.LastLookTime.Value == null) { deleteFlag = true; } else if (DateTime.Now.Subtract(item.LastLookTime.Value).TotalDays > 3 && item.UserID == null) { deleteFlag = true; } if (deleteFlag) { var dirFile = $"/current/images/Order/{item.Id}"; FileHelper.Instance.DelectDir(HttpContext.Current.Server.MapPath(dirFile)); HisdesignFunc.Instance.DeleteHisdesignById(item.Id); } } #endregion } return(new ResultJson { HttpCode = 200, Message = "删除成功!" }); }
static public void CleanCache() { if (Common.isWeb) { return; } string path = GetCachePath(); if (BlankString(path)) { return; } // C#遍历指定文件夹中的所有文件 DirectoryInfo TheFolder = new DirectoryInfo(path); // //遍历文件夹 // foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories()) // this.listBox1.Items.Add(NextFolder.Name); // //遍历文件 foreach (FileInfo NextFile in TheFolder.GetFiles()) { NextFile.Delete(); } // this.listBox2.Items.Add(NextFile.Name); }
private void cleanPicture() { try { string path = Environment.CurrentDirectory + @"\picture"; DirectoryInfo TheFolder = new DirectoryInfo(path); foreach (FileInfo NextFile in TheFolder.GetFiles())//遍历文件 { string fileName = (NextFile.Name); if (fileName == "defaultAvatar.jpg") { continue; } string[] s = fileName.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries); if (s.Length != 2) { NextFile.Delete(); continue; } UInt32 uID = (UInt32)Convert.ToInt32(s[0]); //用户号 string userName = s[1].Replace(".jpg", ""); //姓名 int index = personnelManagement.uIDtoIndex(uID); if ((index < 0) || (personnelManagement.PersonList[index].name != userName)) { NextFile.Delete(); } } } catch (Exception ex) { output(ex.Message); } }
public static void Delete(int day) { if (day > 0) { DateTime keepDay = DateTime.Now.AddDays(-day); //if (string.IsNullOrEmpty(saveFolder)) //{ // DBHelper db = new DBHelper(); // DataTable dt = db.RunTxtDataTable("select * from TN_SyncStrategy"); // saveFolder = dt.Rows[0]["Col_LogFilePath"].ToString(); //} string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "C:\\"); if (Directory.Exists(filePath)) { DirectoryInfo d = new DirectoryInfo(filePath); FileInfo[] fileInfo = d.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { DateTime createtime = NextFile.CreationTime; if (DateTime.Compare(createtime, keepDay) < 0) { AddLog("删除日志文件" + NextFile.Name); NextFile.Delete(); } } } } }
static void DeleteTxt(string path) { DirectoryInfo directory = new DirectoryInfo(path); foreach (var NextFile in directory.GetFiles("*.txt", SearchOption.AllDirectories)) { NextFile.Delete(); } }
static void DeleteExtTxt(DirectoryInfo folder, string pattern) { foreach (var NextFile in folder.GetFiles(pattern + ".txt")) { NextFile.Delete(); } foreach (DirectoryInfo next in folder.GetDirectories()) { DeleteExtTxt(next, pattern); } }
/// <summary> /// 删除文件夹下所有内容 /// </summary> /// <param name="folder"></param> private void DeleteFolder(DirectoryInfo folder) { FileInfo[] fileInfo = folder.GetFiles(); //遍历文件 foreach (FileInfo NextFile in fileInfo) { NextFile.Delete(); } DirectoryInfo[] folders = folder.GetDirectories(); foreach (var item in folders) { DeleteFolder(item); } folder.Delete(); }
public static void ClearManifestFile() { string path = Path.Combine(System.Environment.CurrentDirectory, outputPath); DirectoryInfo TheFolder = new DirectoryInfo(path); float p = 0.2f; //遍历文件夹 foreach (FileInfo NextFile in TheFolder.GetFiles()) { if (NextFile.Name.EndsWith(".manifest")) { p = p > 1.0f ? 0.2f : (p + 0.2f); // Debug.Log("clear file name: "+ NextFile.Name); EditorUtility.DisplayProgressBar("清理manifest", NextFile.Name, p); NextFile.Delete(); } } EditorUtility.ClearProgressBar(); }
private void label2_Click(object sender, EventArgs e) { int num = 0; long length = 0; DirectoryInfo TheFolder = new DirectoryInfo(System.Windows.Forms.Application.StartupPath + "\\bin"); //遍历文件 foreach (FileInfo NextFile in TheFolder.GetFiles()) { if (NextFile.Name.Contains("模板") || NextFile.Name.Contains("~$")) { length += NextFile.Length; NextFile.Delete(); num += 1; } } var value = CountSize(length); MessageBox.Show("删除了" + num.ToString() + "个" + value + "文件"); }
public static void DeleteDirectory(string dir) { DirectoryInfo theFolder = new DirectoryInfo(dir); //删除子文件 FileInfo[] fileInfo = theFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { NextFile.Delete(); } //删除子文件夹 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { DeleteDirectory(NextFolder.FullName); } Directory.Delete(dir); }
//获取本地图片及保存时间如果大于7天删除 public void ClearPic() { //List<string> list = new List<string>(); DirectoryInfo theFolder = new DirectoryInfo(path); string time = ""; string nowTime = GetTimeStamp(DateTime.Now); //遍历文件获取文件名 foreach (FileInfo NextFile in theFolder.GetFiles()) { //list.Add(NextFile.Name); time = PlayerPrefs.GetString(NextFile.Name); if (time != "") { if (int.Parse(nowTime) - int.Parse(time) > 60) { NextFile.Delete(); PlayerPrefs.DeleteKey(NextFile.Name); } } } }
static void ClearPlist(string path, DirectoryInfo theFolder) { if (theFolder == null) { if (!Directory.Exists(path)) //文件夹不存在 { return; } theFolder = new DirectoryInfo(@path); FirstOutDirHead = theFolder.Name; } else { var strFolder = Util.config.PlistLoadDir + "\\" + theFolder.FullName.Remove(0, theFolder.FullName.IndexOf(FirstOutDirHead) + FirstOutDirHead.Length + 1); if (!Directory.Exists(strFolder)) //源文件夹没这个目录,直接删除 { theFolder.Delete(true); return; } } //遍历文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { string strType = NextFile.Extension.ToLower(); if (strType == ".png" || strType == ".jpg" || strType == ".plist") { NextFile.Delete(); } } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { ClearPlist(NextFolder.FullName, NextFolder); } }
private void SearchDir(string path, double k) { try { Invoke(SetLabelMethod, path); if (!path.EndsWith(Util.Separator.ToString())) { path += Util.Separator; } HashSet <string> list = new HashSet <string>(); DirectoryInfo theFolder = new DirectoryInfo(path); DirectoryInfo[] theFolders = null; try { theFolders = theFolder.GetDirectories(); } catch (Exception e) { AddExceptionInfo(e); return; } double ProgressNow = Progress; double step = 1.00 / theFolders.Length * k; int i = 1; //遍历文件夹及子文件夹 foreach (DirectoryInfo NextDir in theFolders) { if (Abort) { return; } if (FixFolder && NextDir.Attributes.IsHidden()) { try { NextDir.Attributes = NormalDir; AddFixedInfo(NextDir.FullName); } catch (Exception e) { AddExceptionInfo(e); } } if (FuzzyCheck || NextDir.Attributes.IsSystem()) { list.Add(NextDir.Name); } } //查杀病毒文件 FileInfo[] theFiles = null; foreach (string Extension in ExtensionList) { try { theFiles = theFolder.GetFiles("*" + Extension, SearchOption.TopDirectoryOnly); } catch (Exception e) { AddExceptionInfo(e); return; } foreach (FileInfo NextFile in theFiles) { if (Abort) { return; } string s = NextFile.Name.Substring(0, NextFile.Name.Length - Extension.Length).TrimEnd(); if (list.Contains(s)) { AddDeleteInfo(NextFile.FullName); NextFile.Delete(); if (!FixFolder) { try { File.SetAttributes(path + s, NormalDir); AddFixedInfo(path + s); } catch (Exception e) { AddExceptionInfo(e); } } } else { //清除多余病毒 foreach (Regex regex in ExtraVirus) { if (regex.IsMatch(s)) { AddDeleteInfo(NextFile.FullName); NextFile.Delete(); } } } } } //隐藏系统文件夹 foreach (string theName in SystemFolder) { if (Directory.Exists(path + theName)) { try { File.SetAttributes(path + theName, SystemHiddenDir); } catch (Exception e) { AddExceptionInfo(e); } } } //扫描子文件夹 if (SubFolder) { foreach (DirectoryInfo NextDir in theFolders) { if (Abort) { return; } SearchDir(path + NextDir.Name + Util.Separator, step); if (step > 1e-2) { Progress = ProgressNow + step * i++; } } } } catch (Exception e) { AddExceptionInfo(e); } }