winrar 压缩文件方法

问题描述:

  我要一些大的文件进行压缩,看了网上有很多类拟的写法。但在我这都存在两个问题。

  1.就是他们都是通过注册表找到rar的exe目录。我安装好winrar后,虽然注册表有,但那个目录一直报一个错。

  2.压缩文件中包含了上几层的目录。

问题分析:

  1.针对第一个问题其实你就指定你机器的目录就行了。何必那么麻烦。

  2.加一个对应的指令就行了。

具体代码如下:

用如下代码前先安装好winrar

/// <summary>
   /// 利用 WinRAR 进行压缩
   /// </summary>
   /// <param name="strRarPatch">待压缩文件目录</param>
   /// <param name="strRarFiles">待压缩文件路径(绝对全路径)</param>
   /// <param name="strPatch">压缩目录(绝对路径)</param>
   /// <param name="strRarName">压缩后的文件名</param>
   public static void SaveRar(string strRarPatch, string strRarFiles, string strPatch, string strRarName)
   {
       String strThe_Info;
       ProcessStartInfo the_StartInfo;
       Process the_Process;
       try
       {
           if (!Directory.Exists(strPatch))
           {
               Directory.CreateDirectory(strPatch);
           }
           //命令参数 df:删除原文件  ep:从名称中排除路径
           //如果压缩文件不指定具体文件夹,只给一个压缩文件,默认会跟原文件放在同一目录
           strThe_Info = string.Format(" a -df -ep {0}\\{1}  {2}  -r", strPatch, strRarName, strRarFiles);
           the_StartInfo = new ProcessStartInfo();
           //从配置信息中找到对应winrar安装目录的exe对应绝对路径:如:C:\Program Files (x86)\WinRAR\WinRAR.exe
           string strWinRarPath =System.Configuration.ConfigurationSettings.AppSettings["WinRarPath"].ToString();
           the_StartInfo.FileName = @strWinRarPath;
           the_StartInfo.Arguments = strThe_Info;
           the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
           //打包文件存放目录
           the_StartInfo.WorkingDirectory = strRarPatch;
           the_Process = new Process();
           the_Process.StartInfo = the_StartInfo;
           the_Process.Start();
           the_Process.WaitForExit();
           the_Process.Close();
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }
利用winrar 进行压缩

 

posted @ 2015-10-28 11:51  丁焕轩  阅读(597)  评论(0编辑  收藏  举报