Method of C calling Rar file and obtaining Rar return value

  • 2021-07-22 11:03:42
  • OfStack

This article illustrates how C # calls Rar file and obtains the return value of Rar. Share it for your reference. The details are as follows:

This program is suitable for C # calls.
The required Rar. exe can be downloaded from WinRar official website.

Button OK click event:


System.Diagnostics.Process process;
private void btnRAR_Click(object sender, EventArgs e)
{
 if (System.IO.File.Exists("Rar.exe"))
 {
  try
  {
   process = new System.Diagnostics.Process();
   process.StartInfo.FileName = "Rar.exe";
   process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
   process.EnableRaisingEvents = true;
   //m<0..5>   Set the compression level (0- Storage ...3- Default ...5- Maximum )
   //rar a -m5 -ag[yyyy Year mm Month dd Day _hh Point mm Points -ss] MyLuoLuo
   process.StartInfo.Arguments = string.Format(@"a -m{0} -ag[yyyy Year mm Month dd Day _hh Point mm Points -ss] {1}\\MyLuoLuo.rar {2}", this.SaveRank.ToString(),this.FileName,this.RarPath); //@"a -m5 -ag[yyyy Year mm Month dd Day _hh Point mm Points -ss] MyLuoLuo.rar D:\Txt";
   process.Exited += new EventHandler(RarStop);
   process.Start();
  }
  catch (Exception ex)
  {
   MessageBox.ShowError(" An error occurred ! Error message :\n" + ex.Message);
  }
 }
}

Determine the return value when the process exits:


private void RarStop(object sender, System.EventArgs e)
{
 switch (process.ExitCode)
 {
  case 0:
   MessageBox.ShowInformation(" Compression succeeded! ");
   break;
  case 1:
   MessageBox.ShowInformation(" There was an error but no fatal error occurred! ");
   break;
  case 2:
   MessageBox.ShowError(" Occurrence 1 A fatal mistake! ");
   break;
  case 3:
   MessageBox.ShowError(" Occurs when decompressing 1 A  CRC  Mistake! ");
   break;
  case 4:
   MessageBox.ShowError(" Attempt to modify the previous use of  'k'  Command to lock the compressed file! ");
   break;
  case 5:
   MessageBox.ShowError(" Error writing to disk! ");
   break;
  case 6:
   MessageBox.ShowError(" Error opening file! ");
   break;
  case 7:
   MessageBox.ShowError(" Wrong command line option! ");
   break;
  case 8:
   MessageBox.ShowError(" There is not enough memory to operate! ");
   break;
  case 9:
   MessageBox.ShowError(" Error creating file! ");
   break;
  case 255:
   MessageBox.ShowError(" User interrupt operation ");
   break;
  default:
   MessageBox.ShowError(" Unknown error! ");
   break;
 }
}

I hope this article is helpful to everyone's C # programming.


Related articles: