把指定的文件压缩指定的rar文件

using System;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using System.Text;
public class DecompressRARTool
{
    /// <summary>
    /// 解压缩指定的rar文件。
    /// </summary>
    /// <param name="rarFileToDecompress">rar文件(绝对路径)。</param>
    /// <param name="directoryToSave">解压缩保存的目录。</param>
    /// <param name="deleteRarFile">解压缩后删除rar文件。</param>
    public void DecompressRAR(string rarFileToDecompress, string directoryToSave, bool deleteRarFile)
    {
        String the_rar;
        RegistryKey the_Reg;
        Object the_Obj;

        the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command");
        the_Obj = the_Reg.GetValue("");
        the_rar = the_Obj.ToString();
        the_Reg.Close();
        the_rar = the_rar.Substring(1, the_rar.Length - 7);


        string winrarExe = the_rar;//需要在指定路径下放入winara.exe的可执行文件在安装目录下可以找到这个文件
        if (new FileInfo(winrarExe).Exists)
        {
            //directoryToSave = CheckDirectoryName(directoryToSave);
            try
            {
                Process p = new Process();
                // 需要启动的程序名
                p.StartInfo.FileName = winrarExe;
                // 参数
                string arguments = @"x -inul -y -o+";
                arguments += " " + rarFileToDecompress + " " + directoryToSave;

                p.StartInfo.Arguments = arguments;

                p.Start();//启动
                while (!p.HasExited)
                {
                }
                p.WaitForExit();
            }
            catch (Exception ee)
            {
                throw new Exception("压缩文件在解压缩的过程中出现了错误!");
            }

            if (deleteRarFile)
            {
                File.Delete(rarFileToDecompress);
            }
        }
        else
        {
            throw new Exception("本机上缺少必须的Winrar.exe文件,不能完成相应操作请联系管理员安装WinRar解压工具!");

        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值