public static void CompressFileToRar(string soruceDir, string rarFileName)
{
Process process = new Process();
try
{
string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
string winrarPath = registryKey.GetValue("").ToString();
registryKey.Close();
string winrarDir = System.IO.Path.GetDirectoryName(winrarPath);
//连同父级目录一块压缩
//String commandOptions = string.Format("a {0} {1} -r", rarFileName, soruceDir);
//只压缩当前文件
String commandOptions = string.Format("a -en -ep1 {0} {1}", rarFileName, soruceDir);
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = System.IO.Path.Combine(winrarDir, "WinRAR.exe");
processStartInfo.Arguments = commandOptions;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
}
catch (Exception ex)
{
}
finally
{
process.Close();
}
}