How to rar/unrar with command line parameters

Elmub picture Elmub · Jul 9, 2015 · Viewed 10.6k times · Source

I've looked through the internet on how winrar's command line parameters work, and this is what I have so far

        void LOCK(string fld, string pw)
    {
        Process p = new Process();
        p.StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";
        p.StartInfo.Arguments = "rar a -p" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
        p.Start();
    }

    void UNLOCK(string fld, string pw)
    {
        Process p = new Process();
        p.StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";
        p.StartInfo.Arguments = "unrar x -p" + pw + " PL_LOCKED_ARCHIVE.rar";
        p.Start();
    }

However it doesn't seem to create any archive anywhere, with a test folder being C:\PicsAndStuff

Answer

Mofi picture Mofi · Jul 10, 2015

The StartInfo you define results in running WinRAR.exe with command line:

C:\Program Files\WinRAR\WinRAR.exe unrar x -p pw PL_LOCKED_ARCHIVE.rar

That is of course wrong as you do not want to run WinRAR.exe with first argument being a reference to console version Rar.exe or UnRAR.exe. The result is most likely an error message because of invalid command rar respectively unrar as the first argument must be a or x for WinRAR.exe.

So first of all you need to correct StartInfo:

void LOCK(string fld, string pw)
{
    Process p = new Process();
    p.StartInfo.FileName = @"C:\Program Files\WinRAR\Rar.exe";
    p.StartInfo.Arguments = "a -p" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
    p.Start();
}

void UNLOCK(string fld, string pw)
{
    Process p = new Process();
    p.StartInfo.FileName = @"C:\Program Files\WinRAR\UnRAR.exe";
    p.StartInfo.Arguments = "x -p" + pw + " PL_LOCKED_ARCHIVE.rar";
    p.Start();
}

Further all commands and switches of console version Rar.exe are briefly explained when simply running Rar.exe without any parameter in a command prompt window. Also UnRAR.exe outputs a brief help if executed without any parameter.

Last but not least there is a complete manual for Rar.exe which of course can also extract files and folders from a RAR archive which makes additional usage of UnRAR.exe useless. The manual is text file Rar.txt in program files folder of WinRAR which you should read from top to bottom. I suggest to build the command line while reading it and test the command line first from within a command prompt window.

Note 1: Rar.exe is shareware. Only UnRAR.exe is freeware.

Note 2: GUI version WinRAR.exe supports more than console version Rar.exe and therefore the list of switches differ slightly. Complete documentation for WinRAR.exe can be found in help of WinRAR opened with Help - Help Topics or pressing key F1. Open in help on tab Contents the item Command line mode and read. WinRAR.exe is also shareware.