public static class RARHelper { public static bool ExistsWinRar() { bool result = false; string key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe"; RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key); if (registryKey != null) { // result = registryKey.GetValue("").ToString(); result = true; } registryKey.Close(); return result; } // <summary> /// 将格式为rar的压缩文件解压到指定的目录 /// </summary> /// <param name="rarFileName">要解压rar文件的路径</param> /// <param name="saveDir">解压后要保存到的目录</param> public static void DeCompressRar(string sourceFilePath, string destinationPath) { try { string key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe"; RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key); var rarexe = registryKey.GetValue("").ToString(); registryKey.Close(); var shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", sourceFilePath, destinationPath); using (Process unrar = new Process()) { ProcessStartInfo startinfo = new ProcessStartInfo(); startinfo.FileName = rarexe; startinfo.Arguments = shellArguments; //设置命令参数 startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口 unrar.StartInfo = startinfo; unrar.Start(); unrar.WaitForExit();//等待解压完成 unrar.Close(); } } catch (System.Exception ex) { } } /// <summary> /// 将目录和文件压缩为rar格式并保存到指定的目录 /// </summary> /// <param name="soruceDir">要压缩的文件夹目录</param> /// <param name="rarFileName">压缩后的rar保存路径</param> public static void CompressRar(string soruceDir, string rarFileName) { try { string key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe"; RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key); var rarexe = registryKey.GetValue("").ToString(); registryKey.Close(); var shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r", rarFileName, soruceDir); using (Process unrar = new Process()) { ProcessStartInfo startinfo = new ProcessStartInfo(); startinfo.FileName = rarexe; startinfo.Arguments = shellArguments; //设置命令参数 startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口 unrar.StartInfo = startinfo; unrar.Start(); unrar.WaitForExit();//等待解压完成 unrar.Close(); } } catch (System.Exception ex) { } } }