Unrar an archive in C#

不问归期 提交于 2019-12-10 19:42:54

问题


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);

回答1:


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




回答2:


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.




回答3:


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



回答4:


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

Install-Package nunrar

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


来源:https://stackoverflow.com/questions/23578704/unrar-an-archive-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!