1、我用到的压缩类
/// <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;
}
2、Windows服务定时压缩备份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();
OleDbCommand OleCmd = new OleDbCommand(Olestring, OleConn);
OleDbDataReader OleRd = OleCmd.ExecuteReader(); while (OleRd.Read())
{
string RarPath = OleRd["BackupPath"].ToString() + @"\" + OleRd["TaskName"].ToString() + "日期" + NowTime.ToString("yyyy年MM月dd日HH时mm分.rar"); if (File.Exists(RarPath) == false)
{
FileWinrar.RAR(OleRd["SourcePath"].ToString(), OleRd["BackupPath"].ToString(), OleRd["TaskName"].ToString(), OleRd["BackupPass"].ToString());
}
} OleConn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} TimerBackupTask.Enabled = true;
}
}
}
3、出现的问题:
(1)需要压缩的文件被占用时,WINRAR进程会卡机,导致其它任务无法继续运行
(2)WINRAR进程在后台启动运行,有时会出现停止的现象
/// <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;
}
2、Windows服务定时压缩备份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();
OleDbCommand OleCmd = new OleDbCommand(Olestring, OleConn);
OleDbDataReader OleRd = OleCmd.ExecuteReader(); while (OleRd.Read())
{
string RarPath = OleRd["BackupPath"].ToString() + @"\" + OleRd["TaskName"].ToString() + "日期" + NowTime.ToString("yyyy年MM月dd日HH时mm分.rar"); if (File.Exists(RarPath) == false)
{
FileWinrar.RAR(OleRd["SourcePath"].ToString(), OleRd["BackupPath"].ToString(), OleRd["TaskName"].ToString(), OleRd["BackupPass"].ToString());
}
} OleConn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} TimerBackupTask.Enabled = true;
}
}
}
3、出现的问题:
(1)需要压缩的文件被占用时,WINRAR进程会卡机,导致其它任务无法继续运行
(2)WINRAR进程在后台启动运行,有时会出现停止的现象
HKEY_LOCAL_MACHINE > SYSTEM > CurrentControlSet > Control > Lsa > limitblankpassworduse 的值修改为0,然后将计划任务里的run only if logged on选项选上。唯一的缺点是用户必须登录才能启动计划任务。我什么都不知道,抄自
测试有效