1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.IO; 5 using System.Linq; 6 using System.IO.Compression; 7 using System.Text; 8 using System.Collections; 9 using System.Diagnostics; 10 using Microsoft.Win32; 11 12 namespace RarHelper 13 { 14 public class RARClass 15 { 16 /// <summary> 17 /// 獲取WinRAR.exe路徑 18 /// </summary> 19 /// <returns>為空則表示未安裝WinRAR</returns> 20 public static string ExistsRAR() 21 { 22 RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe"); 23 //RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command"); 24 string strkey = regkey.GetValue("").ToString(); 25 regkey.Close(); 26 //return strkey.Substring(1, strkey.Length - 7); 27 return strkey; 28 } 29 30 /// <summary> 31 /// 解壓RAR文件 32 /// </summary> 33 /// <param name="rarFilePath">要解壓的文件路徑</param> 34 /// <param name="unrarDestPath">解壓路徑(絕對路徑)</param> 35 public static void UnRAR(string rarFilePath, string unrarDestPath) 36 { 37 string rarexe = ExistsRAR(); 38 if (String.IsNullOrEmpty(rarexe)) 39 { 40 throw new Exception("未安裝WinRAR程序。"); 41 } 42 try 43 { 44 //組合出需要shell的完整格式 45 string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath); 46 47 //用Process調用 48 using (Process unrar = new Process()) 49 { 50 ProcessStartInfo startinfo = new ProcessStartInfo(); 51 startinfo.FileName = rarexe; 52 startinfo.Arguments = shellArguments; //設置命令參數 53 startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隱藏 WinRAR 窗口 54 55 unrar.StartInfo = startinfo; 56 unrar.Start(); 57 unrar.WaitForExit();//等待解壓完成 58 59 unrar.Close(); 60 } 61 } 62 catch 63 { 64 throw; 65 } 66 } 67 68 /// <summary> 69 /// 壓縮為RAR文件 70 /// </summary> 71 /// <param name="filePath">要壓縮的文件路徑(絕對路徑)</param> 72 /// <param name="rarfilePath">壓縮到的路徑(絕對路徑)</param> 73 public static void RAR(string filePath, string rarfilePath, string otherPara = "") 74 { 75 RAR(filePath, rarfilePath, "", "", otherPara); 76 } 77 78 /// <summary> 79 /// 壓縮為RAR文件 80 /// </summary> 81 /// <param name="filePath">要壓縮的文件路徑(絕對路徑)</param> 82 /// <param name="rarfilePath">壓縮到的路徑(絕對路徑)</param> 83 /// <param name="rarName">壓縮后壓縮包名稱</param> 84 public static void RAR(string filePath, string rarfilePath, string rarName, string otherPara = "") 85 { 86 RAR(filePath, rarfilePath, "", rarName, otherPara); 87 } 88 89 /// <summary> 90 /// 壓縮為RAR文件 91 /// </summary> 92 /// <param name="filePath">要壓縮的文件路徑(絕對路徑)</param> 93 /// <param name="rarfilePath">壓縮到的路徑(絕對路徑)</param> 94 /// <param name="rarName">壓縮后壓縮包名稱</param> 95 /// <param name="password">解壓密鑰</param> 96 public static void RAR(string filePath, string rarfilePath, string password, string rarName, string otherPara = "") 97 { 98 string rarexe = ExistsRAR(); 99 if (String.IsNullOrEmpty(rarexe)) 100 { 101 throw new Exception("未安裝WinRAR程序。"); 102 } 103 104 if (!Directory.Exists(filePath)) 105 { 106 //throw new Exception("文件不存在!"); 107 } 108 109 if (String.IsNullOrEmpty(rarName)) 110 { 111 rarName = Path.GetFileNameWithoutExtension(filePath) + ".rar"; 112 } 113 else 114 { 115 if (Path.GetExtension(rarName).ToLower() != ".rar") 116 { 117 rarName += ".rar"; 118 } 119 } 120 121 try 122 { 123 //Directory.CreateDirectory(rarfilePath); 124 //壓縮命令,相當於在要壓縮的文件夾(path)上點右鍵->WinRAR->添加到壓縮文件->輸入壓縮文件名(rarName) 125 string shellArguments; 126 if (String.IsNullOrEmpty(password)) 127 { 128 shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r", rarName, filePath); 129 } 130 else 131 { 132 shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r -p\"{2}\"", rarName, filePath, password); 133 } 134 if (!string.IsNullOrEmpty(otherPara)) 135 { 136 shellArguments = shellArguments + " " + otherPara; 137 } 138 139 using (Process rar = new Process()) 140 { 141 ProcessStartInfo startinfo = new ProcessStartInfo(); 142 startinfo.FileName = rarexe; 143 startinfo.Arguments = shellArguments; //設置命令參數 144 startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隱藏 WinRAR 窗口 145 startinfo.WorkingDirectory = rarfilePath; 146 147 rar.StartInfo = startinfo; 148 rar.Start(); 149 rar.WaitForExit(); //無限期等待進程 winrar.exe 退出 150 rar.Close(); 151 } 152 } 153 catch 154 { 155 throw; 156 } 157 } 158 159 } 160 }
