如何提取对话框选择的 RAR 文件

How to Extract A RAR file selected by dialogbox

我在 C# 中有一段代码可以将 zip 文件解压到特定文件夹。 我想从该代码中提取一个 RAR 文件。 我尝试了其他一些东西,例如 7-zip、IO.Compression,但它不适合我。 这是我用来提取 zip 文件的代码。

DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                ZipFile.ExtractToDirectory(openFileDialog1.FileName, "TestFolder"); 
                MessageBox.Show("ZIP file extracted successfully!");
            }

我想要的,是这样的。 当用户选择 Zip 或 RAR 文件时,此代码会在预先指定的文件夹中提取相关文件。 这是一个 windows 表格申请。 请帮忙。 欢迎任何帮助。 谢谢

RAR 是另一种压缩格式。您应该使用另一个库来处理 RAR 文件,因为 .NET 没有内置任何内容。例如:http://sharpcompress.codeplex.com

来自官方文档页面:"Extract all files from a Rar file to a directory using RarArchive"

using (var archive = RarArchive.Open("Test.rar"))
{
    foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
    {
        entry.WriteToDirectory("D:\temp", new ExtractionOptions()
        {

        });
    }
}