user147685
user147685

Reputation: 469

C# program with WinRAR

I want to design a c# program which can run program a 3rd exe application such as WinRAR. the program will browse for file and when then user click a button, the process to create archive will begin..!

I know using System.Diagnostics.Process.Start method can execute the .exe file. for eg.

Process.Start(@"C:\path\to\file.zip");

GetFile("filename","open winrar to execute the file") I need something like this. I wanna pass the file name to that 3rd application, without need to open winrar. Is it possible? How should i start? Any references/guidance/solution are very thankful.

thank you very much.

//UPDATED

Here is the code to open the WinRAR.exe program else the error message appeared.I pun it in button_click and accept file from txtDest.text using browse. So from here, instead of open the files, i want to to compress directly. I try to change "RAR.exe" or "UNRAR.exe" but it didnt work. It is right?

thank you.

 ProcessStartInfo startInfo = new ProcessStartInfo("WinRAR.exe");
 startInfo.WindowStyle = ProcessWindowStyle.Maximized;
 startInfo.Arguments = txtDest.Text;
 try
 {
   // Start the process with the info we specified.
   using (Process exeProcess = Process.Start(startInfo))
    {
        exeProcess.WaitForExit();
    }
  }
  catch
    {
        MessageBox.Show("Error Open");
    }
  }

Upvotes: 8

Views: 25466

Answers (6)

Wolfzoon
Wolfzoon

Reputation: 437

Yes, I'm ressurrecting a completely dead question here, but I've not seen anyone put up the exact answer you(and until 20 minutes ago I too) want, so let me put 2 and 2 together:

Command line Usage: rar.exe a <target .rar file> <file to rar> {<more files>}
You can make more complicated names, like ones containing spaces, by putting quotation marks around the names. The program you'll probably want is thus:

string targetArchiveName = "archive.rar",
targetFile = "testFile.txt";
ProcessStartInfo startInfo = new ProcessStartInfo("WinRAR.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = string.Format("a \"{0}\" \"{1}\"",
                      targetArchiveName, targetFile);
try
{
  // Start the process with the info we specified.
  using (Process exeProcess = Process.Start(startInfo))
  {
    exeProcess.WaitForExit();
  }
}
catch
{
  {
    MessageBox.Show("Error Open");
  }
}

Upvotes: 8

Havenard
Havenard

Reputation: 27864

WinRAR installation brings its console versions, "RAR.EXE" and "UNRAR.EXE". As console applications made to be fully controlled via command line. You integrate it by preparing the command line and starting the process, optionally hidden so user won't see anything but your program.

Usage:

RAR.EXE a "C:\file to create.rar" "C:\Documents\file to include.jpg"         
"C:\Photos\my picture.jpg" "C:\curriculum.doc" [... as many files you want]

I believe there is a "rarLib" somewhere to procure it more professionally.

Upvotes: 1

Stefan Steiger
Stefan Steiger

Reputation: 82186

What about this one:
http://nunrar.codeplex.com/

Upvotes: 3

Dale
Dale

Reputation: 13024

For this you probably want to use unrar.dll which is the library distributed by RarLabs, the people who make Winrar. It contains all the functionality of WinRAR exposed as a COM interface. I used it recently in a project and it is quite good, exposes methods for opening and browsing archives, as well as compression and decompression.

http://www.rarlab.com/rar_add.htm scroll down to "UnRAR.dll UnRAR dynamic library for Windows software developers."

It comes with a really good set of examples including browsing an archive and API documentation.

Upvotes: 13

Noon Silk
Noon Silk

Reputation: 55082

Start with ProcessStartInfo. There are some good examples there that I would only be repeating here. You may be interested in capturing the output of the program that you execute though, so in that case you'll need to set

psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

Then you may read them from the resulting 'Process' object:

string s = process.StandardOutput.ReadToEnd();

Upvotes: 1

Joshua
Joshua

Reputation: 2109

Its possible if WinRar accepts command line arguments which it almost certainly does.
Here is a good example of how to do this sort or thing, number 4 is closest to what you want I think.

Upvotes: 0

Related Questions