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.StartInf
o.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解壓工具!");
}
}
}