/// <summary>
/// 解压缩
/// </summary>
/// <param name="path">文件解压后的存放路径(绝对)</param>
/// <param name="rarPath">将要解压缩的 .rar 文件的存放目录路径(绝对路径)这个可以是相对路径</param>
/// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)不需要路径,前面个参数已含此路径</param>
public void UnRAR(string path, string rarPath, string rarName)
{
System.Environment.CurrentDirectory = Application.StartupPath; //当前的路径是启动路径
bool flag = false;
string rarexe;
RegistryKey regkey;
Object regvalue;
string cmd;
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);
System.IO.Directory.CreateDirectory(path);
//Thread.Sleep(1000);
//解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹
cmd = string.Format("x {0} {1} -y", rarName, path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Environment.CurrentDirectory = Application.StartupPath; //当前的路径是启动路径
startinfo.WorkingDirectory = rarPath;
//Thread.Sleep(1000);
process = new Process();
// Thread.Sleep(1000);
process.StartInfo = startinfo;
// Thread.Sleep(1000);
process.Start();
// Thread.Sleep(1000);
process.WaitForExit();
if (process.HasExited)
{
//flag = true;
File.Delete(rarPath + rarName); // 解压成功删除元文件
}
else
{
countnu++;
}
process.Close();
listFiles(path);
}
catch (Exception e)
{
//MessageBox.Show("解压缩出现错误");
countnu++;
}
}
在解压缩过程中,如果按照上面异步下载的方法下载下来的.rar压缩包。有些文件时解压不了的,所以根据解压缩以及上一篇提到的下载,在上一篇下载中的第一种方法和本篇的解压缩才是一种完美的结合