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解压工具!");
}
}
}