日期:2014-05-18 浏览次数:21093 次
/// <summary> /// 利用 WinRAR 进行压缩 /// </summary> /// <param name="Path">将要被压缩的文件夹(绝对路径)</param> /// <param name="RarPath">压缩后的 .rar 的存放目录(绝对路径)</param> /// <param name="RarName">压缩文件的名称(包括后缀)</param> /// <param name="RarPass">压缩文件的密码</param> /// <returns>true 或 false。压缩成功返回 true,反之,false。</returns> public static bool RAR(string Path, string RarPath, string RarName, string RarPass) { bool Flag = false; try { string Rarexe = System.Windows.Forms.Application.StartupPath + @"\WinRAR.exe"; //WinRAR.exe 的完整路径 Directory.CreateDirectory(Path); //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName) string Cmd = string.Format("a -ag日期yyyy年mm月dd日hh时mm分 {0} {1} -r -p{2}", RarName, Path, RarPass); //WinRAR 命令参数 ProcessStartInfo Startinfo = new ProcessStartInfo(); Startinfo.FileName = Rarexe; Startinfo.Arguments = Cmd; //设置命令参数 Startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口 Startinfo.WorkingDirectory = RarPath; Process Process = new Process(); Process.StartInfo = Startinfo; Process.Start(); Process.WaitForExit(); //无限期等待进程 winrar.exe 退出 if (Process.HasExited) { Flag = true; } Process.Close(); } catch (Exception e) { throw e; } return Flag; }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.IO; using Microsoft.Win32; using System.Data.OleDb; using System.Threading; namespace ServBackupTask { partial class ServBackupTask : ServiceBase { public ServBackupTask() { InitializeComponent(); } System.Timers.Timer TimerBackupTask = new System.Timers.Timer(30000); protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务。 //使时间控件生效 并且1秒钟调用一次TimerBackupTask_Elapsed() 方法 TimerBackupTask.Elapsed += new System.Timers.ElapsedEventHandler(TimerBackupTask_Elapsed); TimerBackupTask.AutoReset = true; TimerBackupTask.Enabled = true; } protected override void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 } private void TimerBackupTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //暂停计时 TimerBackupTask.Enabled = false; //当前时间 DateTime NowTime = DateTime.Now; //连接字符串 string OleConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + @"\DataBackup.mdb"; //查询语句 string Olestring = "select * from BackupTask Where " + "Cstr(Format(StartTime, 'HH:mm'))='" + NowTime.ToString("HH:mm") + "'"; using (OleDbConnection OleConn = new OleDbConnection(OleConStr)) { try { OleConn.Open()