现在的位置: 首页 > 综合 > 正文

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

2018年06月08日 ⁄ 综合 ⁄ 共 3572字 ⁄ 字号 评论关闭
  1. using System; using System.Data;
  2. using System.Configuration; using System.Web;
  3. using System.Web.Security; using System.Web.UI;
  4. using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
  5. using System.Web.UI.HtmlControls; using Microsoft.Win32;
  6. using System.Diagnostics; /*
  7. * 作者:商金来* 日期:* BLOG:http://hi.baidu.com/huqing7002/blog *
  8. * * */
  9. namespace WebDisk {
  10.     /// <summary>     /// 压缩文件夹
  11.     /// </summary>     public class RarClass
  12.      {         public RarClass()
  13.          {             //
  14.             // TODO: 在此处添加构造函数逻辑             //
  15.          }
  16.          #region 压缩文件         /// <summary>
  17.         /// 压缩文件         /// </summary>
  18.         /// <param name="DFilePath">需要压缩的文件夹或者单个文件</param>         /// <param name="DRARName">生成压缩文件的文件名</param>
  19.         /// <param name="DRARPath">生成压缩文件保存路径</param>         /// <returns></returns>
  20.         public static bool RAR(string DFilePath, string DRARName, string DRARPath)          {
  21.              String the_rar;              RegistryKey the_Reg;
  22.              Object the_Obj;              String the_Info;
  23.              ProcessStartInfo the_StartInfo;              Process the_Process;
  24.             try              {
  25.                  the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command");                  the_Obj = the_Reg.GetValue("");
  26.                  the_rar = the_Obj.ToString();                  the_Reg.Close();
  27.                  the_rar = the_rar.Substring(1, the_rar.Length - 7);                  the_Info = " a     " + " " + DRARName + "   " + DFilePath; //命令 + 压缩后文件名 + 被压缩的文件或者路径
  28.                  the_StartInfo = new ProcessStartInfo();                  the_StartInfo.FileName = the_rar;
  29.                  the_StartInfo.Arguments = the_Info;                  the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  30.                  the_StartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。                  the_Process = new Process();
  31.                  the_Process.StartInfo = the_StartInfo;                  the_Process.Start();
  32.                  the_Process.WaitForExit();//让用户等待进程结束,我这里是要同时下载,否则可以不用写这句                 return true;
  33.              }             catch (Exception ex)
  34.              {                 return false;
  35.              }          }
  36.          #endregion
  37.          #region 解压缩         /// <summary>
  38.         /// 解压缩到指定文件夹         /// </summary>
  39.         /// <param name="RARFilePath">压缩文件存在的目录 </param>         /// <param name="RARFileName">压缩文件名称 </param>
  40.         /// <param name="UnRARFilePath">解压到文件夹</param>         /// <returns></returns>
  41.         public static bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath)          {
  42.             //解压缩              String the_rar;
  43.              RegistryKey the_Reg;              Object the_Obj;
  44.              String the_Info;              ProcessStartInfo the_StartInfo;
  45.              Process the_Process;             try
  46.              {                  the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRar.exe/Shell/Open/Command");
  47.                  the_Obj = the_Reg.GetValue("");                  the_rar = the_Obj.ToString();
  48.                  the_Reg.Close();                  the_rar = the_rar.Substring(1, the_rar.Length - 7);
  49.                  the_Info = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;                  the_StartInfo = new ProcessStartInfo();
  50.                  the_StartInfo.FileName = the_rar;                  the_StartInfo.Arguments = the_Info;
  51.                  the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;                  the_Process = new Process();
  52.                  the_Process.StartInfo = the_StartInfo;                  the_Process.Start();
  53.                 return true;              }
  54.             catch (Exception ex)              {
  55.                 return false;              }
  56.          }
  57.          #endregion
  58.      } }

抱歉!评论已关闭.