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, "rar.exe");
processStartInfo.Arguments = commandOptions;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
}
catch (Exception ex)
{
}
finally
{
process.Close();
}
}