C#调用外部程序实现压缩和解压缩[原]

来源:互联网 发布:java document xml 编辑:程序博客网 时间:2024/05/17 07:35
 
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Web; 
  5. using System.Web.Security; 
  6. using System.Web.UI; 
  7. using System.Web.UI.WebControls; 
  8. using System.Web.UI.WebControls.WebParts; 
  9. using System.Web.UI.HtmlControls; 
  10. using Microsoft.Win32; 
  11. using System.Diagnostics; 
  12. namespace WebDisk 
  13.     /// <summary> 
  14.     /// 压缩文件夹 
  15.     /// </summary> 
  16.     public class RarClass 
  17.     { 
  18.         public RarClass() 
  19.         { 
  20.             // 
  21.             // TODO: 在此处添加构造函数逻辑 
  22.             // 
  23.         } 
  24.         #region 压缩文件 
  25.         /// <summary> 
  26.         /// 压缩文件 
  27.         /// </summary> 
  28.         /// <param name="DFilePath">需要压缩的文件夹或者单个文件</param> 
  29.         /// <param name="DRARName">生成压缩文件的文件名</param> 
  30.         /// <param name="DRARPath">生成压缩文件保存路径</param> 
  31.         /// <returns></returns> 
  32.         public static bool RAR(string DFilePath, string DRARName, string DRARPath) 
  33.         { 
  34.             String the_rar; 
  35.             RegistryKey the_Reg; 
  36.             Object the_Obj; 
  37.             String the_Info; 
  38.             ProcessStartInfo the_StartInfo; 
  39.             Process the_Process; 
  40.             try 
  41.             { 
  42.                 the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command"); 
  43.                 the_Obj = the_Reg.GetValue(""); 
  44.                 the_rar = the_Obj.ToString(); 
  45.                 the_Reg.Close(); 
  46.                 the_rar = the_rar.Substring(1, the_rar.Length - 7); 
  47.                 the_Info = " a    " + " " + DRARName + "  " + DFilePath; //命令 + 压缩后文件名 + 被压缩的文件或者路径 
  48.                 the_StartInfo = new ProcessStartInfo(); 
  49.                 the_StartInfo.FileName = the_rar; 
  50.                 the_StartInfo.Arguments = the_Info; 
  51.                 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
  52.                 the_StartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。 
  53.                 the_Process = new Process(); 
  54.                 the_Process.StartInfo = the_StartInfo; 
  55.                 the_Process.Start(); 
  56.                 the_Process.WaitForExit();//让用户等待进程结束,我这里是要同时下载,否则可以不用写这句 
  57.                 return true
  58.             } 
  59.             catch (Exception ex) 
  60.             { 
  61.                 return false
  62.             } 
  63.         } 
  64.         #endregion 
  65.         #region 解压缩 
  66.         /// <summary> 
  67.         /// 解压缩到指定文件夹  
  68.         /// </summary> 
  69.         /// <param name="RARFilePath">压缩文件存在的目录 </param> 
  70.         /// <param name="RARFileName">压缩文件名称 </param> 
  71.         /// <param name="UnRARFilePath">解压到文件夹</param> 
  72.         /// <returns></returns> 
  73.         public static bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath) 
  74.         { 
  75.             //解压缩 
  76.             String the_rar; 
  77.             RegistryKey the_Reg; 
  78.             Object the_Obj; 
  79.             String the_Info; 
  80.             ProcessStartInfo the_StartInfo; 
  81.             Process the_Process; 
  82.             try 
  83.             { 
  84.                 the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRar.exe/Shell/Open/Command"); 
  85.                 the_Obj = the_Reg.GetValue(""); 
  86.                 the_rar = the_Obj.ToString(); 
  87.                 the_Reg.Close(); 
  88.                 the_rar = the_rar.Substring(1, the_rar.Length - 7); 
  89.                 the_Info = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath; 
  90.                 the_StartInfo = new ProcessStartInfo(); 
  91.                 the_StartInfo.FileName = the_rar; 
  92.                 the_StartInfo.Arguments = the_Info; 
  93.                 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
  94.                 the_Process = new Process(); 
  95.                 the_Process.StartInfo = the_StartInfo; 
  96.                 the_Process.Start(); 
  97.                 return true
  98.             } 
  99.             catch (Exception ex) 
  100.             { 
  101.                 return false
  102.             } 
  103.         } 
  104.         #endregion 
  105.     }