سؤال

I want to silent extract of .rar archive. .Net haven't any Rar classes in IO.Compression, so i'd like to use cmd (it's better, than external dll). It extracts, but not in silent mode. What am I doing wrong?

const string source = "D:\\22.rar";
string destinationFolder = source.Remove(source.LastIndexOf('.'));
string arguments = string.Format(@"x -s ""{0}"" *.* ""{1}\""", source, destinationFolder);
Process.Start("winrar", arguments);
هل كانت مفيدة؟

المحلول

I use this bit of code myself (your code added):

const string source = "D:\\22.rar";
string destinationFolder = source.Remove(source.LastIndexOf('.'));
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "\"C:\\Program Files\\WinRAR\\winrar.exe\"";
p.StartInfo.Arguments = string.Format(@"x -s ""{0}"" *.* ""{1}\""", source, destinationFolder);
p.Start();
p.WaitForExit();

That will launch the WinRAR program in the background and return control to you once the file has completed un-rar-ing.

Hope that can help

نصائح أخرى

Use SharpCompress https://github.com/adamhathcock/sharpcompress/blob/master/USAGE.md there is also a nuget package for easy installation.

Install-Package sharpcompress -Version 0.22.0

Alternatively, you can embed with your application the tool called unRAR, the small executable developed by the WinRAR's developers (official download).

This utility is free, thus your users won't need to have a WinRAR license to use your application. :) It can be executed using the same way txtechhelp described.

Use NUnrar, there is also a nuget package for easy installation.

Install-Package nunrar

NUnrar.Archive.RarArchive.WriteToDirectory(fileName,destinationfileName);

As Mihail Shishkov suggested it's possible to unrar files with SharpCompress library.

dotnet add package SharpCompress

Here is a basic usage example:

var archive = RarArchive.Open("/Downloads/archive.rar");

foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
    entry.WriteToDirectory("/Downloads");

I tried to use it in .NET 6 application under macOS and it works fine.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top