using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Linq;
using System.IO.Compression;
using System.Text;
using System.Collections;
using System.Diagnostics;
using Microsoft.Win32; namespace RarHelper
{
public class RARClass
{
/// <summary>
/// 获取WinRAR.exe路径
/// </summary>
/// <returns>为空则表示未安装WinRAR</returns>
public static string ExistsRAR()
{
RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
//RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
string strkey = regkey.GetValue("").ToString();
regkey.Close();
//return strkey.Substring(1, strkey.Length - 7);
return strkey;
} /// <summary>
/// 解压RAR文件
/// </summary>
/// <param name="rarFilePath">要解压的文件路径</param>
/// <param name="unrarDestPath">解压路径(绝对路径)</param>
public static void UnRAR(string rarFilePath, string unrarDestPath)
{
string rarexe = ExistsRAR();
if (String.IsNullOrEmpty(rarexe))
{
throw new Exception("未安装WinRAR程序。");
}
try
{
//组合出需要shell的完整格式
string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath); //用Process调用
using (Process unrar = new Process())
{
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = shellArguments; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口 unrar.StartInfo = startinfo;
unrar.Start();
unrar.WaitForExit();//等待解压完成 unrar.Close();
}
}
catch
{
throw;
}
} /// <summary>
/// 压缩为RAR文件
/// </summary>
/// <param name="filePath">要压缩的文件路径(绝对路径)</param>
/// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
public static void RAR(string filePath, string rarfilePath, string otherPara = "")
{
RAR(filePath, rarfilePath, "", "", otherPara);
} /// <summary>
/// 压缩为RAR文件
/// </summary>
/// <param name="filePath">要压缩的文件路径(绝对路径)</param>
/// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
/// <param name="rarName">压缩后压缩包名称</param>
public static void RAR(string filePath, string rarfilePath, string rarName, string otherPara = "")
{
RAR(filePath, rarfilePath, "", rarName, otherPara);
} /// <summary>
/// 压缩为RAR文件
/// </summary>
/// <param name="filePath">要压缩的文件路径(绝对路径)</param>
/// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
/// <param name="rarName">压缩后压缩包名称</param>
/// <param name="password">解压密钥</param>
public static void RAR(string filePath, string rarfilePath, string password, string rarName, string otherPara = "")
{
string rarexe = ExistsRAR();
if (String.IsNullOrEmpty(rarexe))
{
throw new Exception("未安装WinRAR程序。");
} if (!Directory.Exists(filePath))
{
//throw new Exception("文件不存在!");
} if (String.IsNullOrEmpty(rarName))
{
rarName = Path.GetFileNameWithoutExtension(filePath) + ".rar";
}
else
{
if (Path.GetExtension(rarName).ToLower() != ".rar")
{
rarName += ".rar";
}
} try
{
//Directory.CreateDirectory(rarfilePath);
//压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
string shellArguments;
if (String.IsNullOrEmpty(password))
{
shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r", rarName, filePath);
}
else
{
shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r -p\"{2}\"", rarName, filePath, password);
}
if (!string.IsNullOrEmpty(otherPara))
{
shellArguments = shellArguments + " " + otherPara;
} using (Process rar = new Process())
{
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = shellArguments; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
startinfo.WorkingDirectory = rarfilePath; rar.StartInfo = startinfo;
rar.Start();
rar.WaitForExit(); //无限期等待进程 winrar.exe 退出
rar.Close();
}
}
catch
{
throw;
}
} }
}

c#调用WinRAR软件压缩和解压文件的更多相关文章

  1. C#压缩和解压文件

    这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...

  2. 【转】Java压缩和解压文件工具类ZipUtil

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  3. C# 压缩和解压文件(SharpZipLib)

    先从网上下载ICSharpCode.SharpZipLib.dll类库 将文件或文件夹压缩为zip,函数如下 /// <summary> /// 压缩文件 /// </summary ...

  4. 压缩和解压文件:tar gzip bzip2 compress(转)

    tar[必要参数][选择参数][文件] 压缩:tar -czvf filename.tar.gz targetfile解压:tar -zxvf filename.tar.gz参数说明: -c 建立新的 ...

  5. linux 压缩和解压文件

    一.压缩:20120715文件下面所有的文件 如下: tar -zcvf 20120715.tar.gz  20120715* 二.解压20120715.tar.gz压缩包 如下: tar -xzvf ...

  6. 使用GZipStream压缩和解压文件

    最近做了一个用.NET里的GZipStream压缩解压缩gzip文件的小程序. GZipStream在System.IO.Compression底下,使用起来也很简单.虽然GZipStream是Str ...

  7. linux压缩和解压文件命令

    tar  解包:tar zxvf filename.tar  打包:tar czvf filename.tar dirnamegz命令  解压1:gunzip filename.gz  解压2:gzi ...

  8. Android_JarZip压缩和解压文件

        本文资料来自<android开发权威指南> AndroidSDK中提供了java.util.jar和java.util.zip包中的若干类和接口来完成. 压缩文件基本步骤: 1.创 ...

  9. metro压缩和解压文件

    在1.zip中增加一张新图片StorageFile jpg = await KnownFolders.PicturesLibrary.GetFileAsync("1.jpg"); ...

随机推荐

  1. linux的文件权限分析

    windows中,文件的类型是根据后缀名来确定的,但是linux则是根据标志来确定的,查看一个文件的权限的命令是 ls -l #查看文件的权限 文件的权限结构如图: ①第一部分:10个字符(第1位表示 ...

  2. 使用bottle进行web开发(1):hello world

    为什么使用bottle?因为简单,就一个py文件,和其他模块没有依赖,3000多行代码. http://www.bottlepy.org/docs/dev/ 既然开始学习,就安装它吧. pip3 in ...

  3. Python与数据库[1] -> 数据库接口/DB-API[2] -> SQL Server 适配器

    SQL_Server适配器 / SQL_Server Adapter 1 环境配置 / Environment Configuration 安装SQL_Server的Python适配器包 pip in ...

  4. Codeforces 825F - String Compression

    825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...

  5. [POI2014]FarmCraft

    题目大意: 一个$n(n\le5\times10^5)$个点的树,每个点有一个权值$c_i$,从$1$出发进行欧拉遍历,每个单位时间移动一条边,记每个点$i$被访问到的时间是$t_i$,问最后$\ma ...

  6. !!!!Linux系统开发 系列 4 进程资源 环境 fork()子进程 wait() waitpid()僵尸 孤儿进程

    http://990487026.blog.51cto.com/10133282/1834893

  7. 利用osql/ocmd批处理批量执行sql文件

    原文:利用osql/ocmd批处理批量执行sql文件 上周在测试环境建了几十张表,保存了.sql文件,准备在正式环境重建的时候懒得一个个打开建了,做一在网上搜寻了一下,果然有简单点的方法. 利用osq ...

  8. XCode工程内多Targets教程

    作者  透明de面具 原帖地址  http://www.cocoachina.com/bbs/read.php?tid-10972-fpage-0-toread--page-1.html    相信很 ...

  9. js封装的一行半显示省略号。(字数自由控制)

    $(function() { //控制一行半隐藏 (function ($) { $.fn.displayPart = function (opts) { $(this).each(function ...

  10. android下socket编程问题:服务器关闭时,客户端发送请求的异常处理

    我用socket分别创建了一个服务器和一个客户端. 当服务器程序运行时,客户端和服务器发送接收数据是OK的. 但是,如果服务器程序关闭以后,客户端仍然发送请求的话,会抛出一个IOException.但 ...