using Microsoft.Win32;
using System.Diagnostics;
namespace Masuit.Tools.Files
{
///
/// WinRAR压缩操作
///
public static class WinrarHelper
{
#region 压缩
///
/// 压缩
///
/// 要解压的文件名
/// 要压缩的文件目录
/// 初始目录
public static void Rar(string zipname, string zippath, string dirpath)
{
_theReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
if (_theReg != null)
{
_theObj = _theReg.GetValue("");
_theRar = _theObj.ToString();
_theReg?.Close();
}
_theRar = _theRar.Substring(1, _theRar.Length - 7);
_theInfo = " a " + zipname + " " + zippath;
_theStartInfo = new ProcessStartInfo
{
FileName = _theRar,
Arguments = _theInfo,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = dirpath
};
_theProcess = new Process
{
StartInfo = _theStartInfo
};
_theProcess.Start();
}
#endregion
#region 解压缩
///
/// 解压缩
///
/// 要解压的文件名
/// 要解压的文件路径
public static void UnRar(string zipname, string zippath)
{
_theReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
if (_theReg != null)
{
_theObj = _theReg.GetValue("");
_theRar = _theObj.ToString();
_theReg.Close();
}
_theRar = _theRar.Substring(1, _theRar.Length - 7);
_theInfo = " X " + zipname + " " + zippath;
_theStartInfo = new ProcessStartInfo
{
FileName = _theRar,
Arguments = _theInfo,
WindowStyle = ProcessWindowStyle.Hidden
};
_theProcess = new Process
{
StartInfo = _theStartInfo
};
_theProcess.Start();
}
#endregion
#region 私有变量
private static string _theRar;
private static RegistryKey _theReg;
private static object _theObj;
private static string _theInfo;
private static ProcessStartInfo _theStartInfo;
private static Process _theProcess;
#endregion
}
}