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

  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. /*
  13.  * 作者:Tom Hu
  14.  * 日期:2008-10-10
  15.  * BLOG:http://hi.baidu.com/huqing7002/blog
  16.  * 
  17.  * 
  18.  * */
  19. namespace WebDisk
  20. {
  21.     /// <summary>
  22.     /// 压缩文件夹
  23.     /// </summary>
  24.     public class RarClass
  25.     {
  26.         public RarClass()
  27.         {
  28.             //
  29.             // TODO: 在此处添加构造函数逻辑
  30.             //
  31.         }
  32.         #region 压缩文件
  33.         /// <summary>
  34.         /// 压缩文件
  35.         /// </summary>
  36.         /// <param name="DFilePath">需要压缩的文件夹或者单个文件</param>
  37.         /// <param name="DRARName">生成压缩文件的文件名</param>
  38.         /// <param name="DRARPath">生成压缩文件保存路径</param>
  39.         /// <returns></returns>
  40.         public static bool RAR(string DFilePath, string DRARName, string DRARPath)
  41.         {
  42.             String the_rar;
  43.             RegistryKey the_Reg;
  44.             Object the_Obj;
  45.             String the_Info;
  46.             ProcessStartInfo the_StartInfo;
  47.             Process the_Process;
  48.             try
  49.             {
  50.                 the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command");
  51.                 the_Obj = the_Reg.GetValue("");
  52.                 the_rar = the_Obj.ToString();
  53.                 the_Reg.Close();
  54.                 the_rar = the_rar.Substring(1, the_rar.Length - 7);
  55.                 the_Info = " a    " + " " + DRARName + "  " + DFilePath; //命令 + 压缩后文件名 + 被压缩的文件或者路径
  56.                 the_StartInfo = new ProcessStartInfo();
  57.                 the_StartInfo.FileName = the_rar;
  58.                 the_StartInfo.Arguments = the_Info;
  59.                 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  60.                 the_StartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。
  61.                 the_Process = new Process();
  62.                 the_Process.StartInfo = the_StartInfo;
  63.                 the_Process.Start();
  64.                 the_Process.WaitForExit();//让用户等待进程结束,我这里是要同时下载,否则可以不用写这句
  65.                 return true;
  66.             }
  67.             catch (Exception ex)
  68.             {
  69.                 return false;
  70.             }
  71.         }
  72.         #endregion
  73.         #region 解压缩
  74.         /// <summary>
  75.         /// 解压缩到指定文件夹 
  76.         /// </summary>
  77.         /// <param name="RARFilePath">压缩文件存在的目录 </param>
  78.         /// <param name="RARFileName">压缩文件名称 </param>
  79.         /// <param name="UnRARFilePath">解压到文件夹</param>
  80.         /// <returns></returns>
  81.         public static bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath)
  82.         {
  83.             //解压缩
  84.             String the_rar;
  85.             RegistryKey the_Reg;
  86.             Object the_Obj;
  87.             String the_Info;
  88.             ProcessStartInfo the_StartInfo;
  89.             Process the_Process;
  90.             try
  91.             {
  92.                 the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRar.exe/Shell/Open/Command");
  93.                 the_Obj = the_Reg.GetValue("");
  94.                 the_rar = the_Obj.ToString();
  95.                 the_Reg.Close();
  96.                 the_rar = the_rar.Substring(1, the_rar.Length - 7);
  97.                 the_Info = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
  98.                 the_StartInfo = new ProcessStartInfo();
  99.                 the_StartInfo.FileName = the_rar;
  100.                 the_StartInfo.Arguments = the_Info;
  101.                 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  102.                 the_Process = new Process();
  103.                 the_Process.StartInfo = the_StartInfo;
  104.                 the_Process.Start();
  105.                 return true;
  106.             }
  107.             catch (Exception ex)
  108.             {
  109.                 return false;
  110.             }
  111.         }
  112.         #endregion
  113.     }
  114. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值