c#调用WinRAR软件压缩和解压文件

  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 }

 

转载于:https://www.cnblogs.com/lyd2016/p/5964687.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值