Search code examples
c#.netcommand-linecmdcompression

Unrar an archive in C#


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

Solution

  • 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