C# 启动WinRAR定时执行Web日志压缩清理
2013-04-13 11:23
330 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Timers; namespace WinCodeTest { /// <summary> /// Web日志文件备份 /// </summary> class FileZip { static List<string> listFile = new List<string>(); public static void ZipLog() { // Create a new Timer with Interval set to 10 seconds. System.Timers.Timer aTimer = new System.Timers.Timer(1000); aTimer.Elapsed = new ElapsedEventHandler(RunZip); // Only raise the event the first time Interval elapses. aTimer.AutoReset = true; aTimer.Enabled = true; Console.WriteLine("The Task Is Running... write 'q' exit"); while (Console.Read() != 'q') ; } /// <summary> /// 遍历文件夹将所有文件进行压缩 /// </summary> private static void RunZip(object source, ElapsedEventArgs e) { //存放日志文件的目录 string filePath = @"D:\WebLog\"; DateTime formatDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); DateTime startTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 02:10:00")); //定时执行时间判断 double ts = (formatDate - startTime).TotalSeconds; if (ts == 0) { GetAllFiles(filePath); foreach (string file in listFile) { Console.WriteLine(DateTime.Now "-=|备份:" file); ZipFile(file); } Console.WriteLine(DateTime.Now "任务结束"); } } /// <summary> /// 遍历目录下所有文件 /// </summary> /// <param name="filePath"></param> private static void GetAllFiles(string filePath) { //递归遍历所有子文件夹 string[] dir = Directory.GetDirectories(filePath); for (int n = 0; n < dir.Length; n ) { GetAllFiles(dir ); } //保存文件夹内的文件路径 log文件 string[] file = Directory.GetFiles(filePath,"*.log"); for (int i = 0; i < file.Length; i ) { FileInfo fi = new FileInfo(file[i]); //将两天前的日志加入压缩文件列表 if (fi.LastWriteTime < DateTime.Now.AddDays(-2)) { listFile.Add(file[i]); } } //删除25天前的备份文件 rar文件 string[] fileRar = Directory.GetFiles(filePath, "*.rar"); for (int i = 0; i < fileRar.Length; i ) { FileInfo fi = new FileInfo(fileRar[i]); if (fi.LastWriteTime < DateTime.Now.AddDays(-25)) { Console.WriteLine(DateTime.Now "-=|删除:" fileRar[i]); fi.Delete(); } } } /// <summary> /// 根据文件路径压缩[删除源文件(-df);不包含文件路径(-ep)] /// </summary> /// <param name="str">文件路径</param> /// <returns></returns> private static string ZipFile(string strFileName) { string fullFileName = strFileName; System.IO.FileInfo fi = new System.IO.FileInfo(fullFileName); string FileName = @"C:\Program Files\WinRAR\Rar.exe"; //Winrar软件的安装目录,也可以将'rar.exe'放在程序的运行目录,不过需要更改一下路径 //执行压缩的命令,可以查看winrar软件的帮助文档,里面有详细的说明 string command = "-df -ep -tl -or a " fi.Directory @"\" fi.Name.Replace(fi.Extensio 4000 n, "") ".rar " fullFileName; System.Diagnostics.Process p = new System.Diagnostics.Process(); string ResultStr; p.StartInfo.FileName = FileName; p.StartInfo.Arguments = " " command; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.ErrorDialog = false; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; try { p.Start(); ResultStr = p.StandardError.ReadToEnd(); p.Close(); return ResultStr; } catch (Exception ex) { return ex.Message.ToString(); } } } }
相关文章推荐
- 可定时执行的Web日志自动压缩清理程序
- C#实现Web应用程序定时启动任务
- C#定时执行某一个功能一(sqlite导出xml并压缩)
- C#实现Web应用程序定时启动任务
- web项目简单的项目启动时执行定时任务
- Java Web应用启动后执行定时任务-ScheduledThreadPoolExecutor
- C#(Web)实现定时启动任务
- C#web中如何在程序里定时启动任务[转]
- C#web中如何在程序里定时启动任务[转]
- 利用process.start()启动WinRAR.exe执行压缩命令在本地运行正常,放到服务器时WinRAR.exe也正常启动了,但没有成功完成压缩指令
- Tomcat启动后加载两次web.xml的问题(因为spring定时任务执行了俩次,引出此问题)
- 清理oracle10g日志----shell脚本定时任务执行rman
- 日志定时压缩清理shell脚本(日志文件后缀格式如xxxxx.log.2017-07-10)
- web启动后台线程定时执行
- apss asp.net C# web 定时执行程序
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- 自动启动rc.local、定时执行crontab的日志文件
- C#调用WinRar执行rar、zip压缩的方法
- Linux应用--日志定时清理
- ASP.NET(C#) 定时执行一段代码