c# rar解压大小_C#利用WinRAR实现压缩和解压缩
usingSystem;usingMicrosoft.Win32;usingSystem.Diagnostics;usingSystem.IO;namespaceMSCL
{///
///壓縮解壓類///
public classZipHelper
{///
///利用 WinRAR 進(jìn)行壓縮///
/// 將要被壓縮的文件夾(絕對(duì)路徑)
/// 壓縮后的 .rar 的存放目錄(絕對(duì)路徑)
/// 壓縮文件的名稱(包括后綴)
/// true 或 false。壓縮成功返回 true,反之,false。
public bool RAR(string path, string rarPath, stringrarName)
{bool flag = false;string rarexe; //WinRAR.exe 的完整路徑
RegistryKey regkey; //注冊(cè)表鍵
Object regvalue; //鍵值
string cmd; //WinRAR 命令參數(shù)
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);//壓縮命令,相當(dāng)于在要壓縮的文件夾(path)上點(diǎn)右鍵->WinRAR->添加到壓縮文件->輸入壓縮文件名(rarName)
cmd = string.Format("a {0} {1} -r",
rarName,
path);
startinfo= newProcessStartInfo();
startinfo.FileName=rarexe;
startinfo.Arguments= cmd; //設(shè)置命令參數(shù)
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隱藏 WinRAR 窗口
startinfo.WorkingDirectory=rarPath;
process= newProcess();
process.StartInfo=startinfo;
process.Start();
process.WaitForExit();//無(wú)限期等待進(jìn)程 winrar.exe 退出
if(process.HasExited)
{
flag= true;
}
process.Close();
}catch(Exception e)
{throwe;
}returnflag;
}///
///利用 WinRAR 進(jìn)行解壓縮///
/// 文件解壓路徑(絕對(duì))
/// 將要解壓縮的 .rar 文件的存放目錄(絕對(duì)路徑)
/// 將要解壓縮的 .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);//解壓縮命令,相當(dāng)于在要壓縮文件(rarName)上點(diǎn)右鍵->WinRAR->解壓到當(dāng)前文件夾
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;
}
}
}
總結(jié)
以上是生活随笔為你收集整理的c# rar解压大小_C#利用WinRAR实现压缩和解压缩的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为mate20指纹识别在哪里
- 下一篇: 4 angular 重构 项目_c# –