Using System; Using System.Collections.Generic; Using System.Text; --------------using Using System.Diagnostics; Using Microsoft.Win32; Using System.IO; <summary> Name:stone Datetime:2011/12/31 16:39:26 Description:winrar compression </summary> public class Winrarcsharp { WinRAR Install registry key Private Const string Winrar_key = @ "Winrar.zipshellopencommand"; <summary> Using WinRAR to compress </summary> <param name= "path" > folder to be Compressed (absolute path) </param> <param name= "Rarpath" > Storage directory (absolute path) of compressed. RAR </param> <param name= "Rarname" > Compressed file name (including suffix) </param> <returns>true or False. Compression successfully returns TRUE, otherwise, false. </returns> public bool RAR (string path, String Rarpath, String rarname) { BOOL flag = FALSE; String Rarexe; Full path to WinRAR.exe RegistryKey RegKey; Registry keys Object Regvalue; Key value string cmd; WinRAR Command Parameters ProcessStartInfo StartInfo; Process process; Try { RegKey = Registry.ClassesRoot.OpenSubKey (Winrar_key); Regvalue = RegKey. GetValue (""); The key value is "D:program FilesWinRARWinRAR.exe" "%1" Rarexe = Regvalue. ToString (); RegKey. Close (); Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7); D:program FilesWinRARWinRAR.exe Directory.CreateDirectory (path); Compression command, which is equivalent to right-clicking on the folder (path) to be compressed->winrar-> add to compressed file-> input compressed filename (rarname) cmd = string. Format (' a {0} {1}-R ', Rarname, path); StartInfo = new ProcessStartInfo (); StartInfo. FileName = Rarexe; StartInfo. Arguments = cmd; Set command parameters StartInfo. WindowStyle = Processwindowstyle.hidden; Hide WinRAR window StartInfo. WorkingDirectory = Rarpath; Process = new process (); Process. StartInfo = StartInfo; Process. Start (); Process. WaitForExit (); Wait indefinitely for process Winrar.exe exit if (process. hasexited) { Flag = true; } Process. Close (); } catch (Exception e) { Throw e; } return flag; } <summary> Using WinRAR to decompress </summary> <param name= "path" > File decompression Path (absolute) </param> <param name= "Rarpath" > Storage directory (absolute path) of the. rar file to be uncompressed </param> <param name= "Rarname" > the. rar file name (including suffix) that will be uncompressed </param> <returns>true or False. The decompression successfully returns TRUE, otherwise, false. </returns> public bool Unrar (string path, String Rarpath, String rarname) { BOOL flag = FALSE; String Rarexe; RegistryKey RegKey; Object Regvalue; string cmd; ProcessStartInfo StartInfo; Process process; Try { RegKey = Registry.ClassesRoot.OpenSubKey (Winrar_key); Regvalue = RegKey. GetValue (""); Rarexe = Regvalue. ToString (); RegKey. Close (); Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7); Directory.CreateDirectory (path); Extract the command, which is equivalent to the right key on the compressed file (rarname)->winrar-> extract to the current folder cmd = string. Format (' x {0} {1}-y ', Rarname, path); StartInfo = new ProcessStartInfo (); StartInfo. FileName = Rarexe; StartInfo. Arguments = cmd; StartInfo. WindowStyle = Processwindowstyle.hidden; StartInfo. WorkingDirectory = Rarpath; Process = new process (); Process. StartInfo = StartInfo; Process. Start (); Process. WaitForExit (); if (process. hasexited) { Flag = true; } Process. Close (); } catch (Exception e) { Throw e; } return flag; } } |