c# rar解压大小_C#利用WinRAR实现压缩和解压缩
usingSystem;usingMicrosoft.Win32;usingSystem.Diagnostics;usingSystem.IO;namespaceMSCL
{///
///压缩解压类///
public classZipHelper
{///
///利用 WinRAR 进行压缩///
/// 将要被压缩的文件夹(绝对路径)
/// 压缩后的 .rar 的存放目录(绝对路径)
/// 压缩文件的名称(包括后缀)
/// true 或 false。压缩成功返回 true,反之,false。
public bool RAR(string path, string rarPath, stringrarName)
{bool flag = false;string rarexe; //WinRAR.exe 的完整路径
RegistryKey regkey; //注册表键
Object regvalue; //键值
string cmd; //WinRAR 命令参数
ProcessStartInfo startinfo;
Process process;try{
regkey= Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regvalue= regkey.GetValue("WinRAR"); //键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1"
rarexe =regvalue.ToString();
regkey.Close();
rarexe= rarexe.Substring(1, rarexe.Length - 7); //d:\Program Files\WinRAR\WinRAR.exe
Directory.CreateDirectory(path);//压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
cmd = string.Format("a {0} {1} -r",
rarName,
path);
startinfo= newProcessStartInfo();
startinfo.FileName=rarexe;
startinfo.Arguments= cmd; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
startinfo.WorkingDirectory=rarPath;
process= newProcess();
process.StartInfo=startinfo;
process.Start();
process.WaitForExit();//无限期等待进程 winrar.exe 退出
if(process.HasExited)
{
flag= true;
}
process.Close();
}catch(Exception e)
{throwe;
}returnflag;
}///
///利用 WinRAR 进行解压缩///
/// 文件解压路径(绝对)
/// 将要解压缩的 .rar 文件的存放目录(绝对路径)
/// 将要解压缩的 .rar 文件名(包括后缀)
/// true 或 false。解压缩成功返回 true,反之,false。
public bool UnRAR(string path, string rarPath, stringrarName)
{bool flag = false;stringrarexe;
RegistryKey regkey;
Object regvalue;stringcmd;
ProcessStartInfo startinfo;
Process process;try{
regkey= Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regvalue= regkey.GetValue("");
rarexe=regvalue.ToString();
regkey.Close();
rarexe= rarexe.Substring(1, rarexe.Length - 7);
Directory.CreateDirectory(path);//解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹
cmd = string.Format("x {0} {1} -y",
rarName,
path);
startinfo= newProcessStartInfo();
startinfo.FileName=rarexe;
startinfo.Arguments=cmd;
startinfo.WindowStyle=ProcessWindowStyle.Hidden;
startinfo.WorkingDirectory=rarPath;
process= newProcess();
process.StartInfo=startinfo;
process.Start();
process.WaitForExit();if(process.HasExited)
{
flag= true;
}
process.Close();
}catch(Exception e)
{throwe;
}returnflag;
}
}
}
总结
以上是生活随笔为你收集整理的c# rar解压大小_C#利用WinRAR实现压缩和解压缩的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 华为mate20指纹识别在哪里
- 下一篇: 4 angular 重构 项目_c# –