Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the "ERRORLEVEL" variable set by a command line scanner in my C# program?

In my website I want to virus-check any uploaded files before saving them into my database. So I save the file to the local directory then kick-off a command-line scanner process from inside my C# program. Here is the code I use:

  string pathToScannerProgram = Path.Combine(virusCheckFolder, "scan.exe");
  ProcessStartInfo startInfo = new ProcessStartInfo();
  startInfo.FileName = pathToScannerProgram;
  startInfo.Arguments = String.Format("\"{0}\" /FAM /DAM", fileToScanPath);
  startInfo.RedirectStandardOutput = true;
  startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  startInfo.UseShellExecute = false;

  using (Process process = new Process())
  {
    process.StartInfo = startInfo;
    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    string errorLevel = Environment.GetEnvironmentVariable("ERRORLEVEL");
    process.WaitForExit();
  }

My problem is that Environment.GetEnvironmentVariable("ERRORLEVEL") is always returning null. It should be returning a number. So how do I get the "ERRORLEVEL" set by the command line scanner in my C# program?

like image 510
Colin Avatar asked Jan 11 '10 14:01

Colin


People also ask

How do I set the errorlevel after every command?

A .CMD batch script will set/reset the ERRORLEVEL after every command that you run [source] Mark Zbikowski (MSFT). Here's an example script with an error check after each command: command1 if %errorlevel% EQU 0 (echo OK ) Else ( Echo ERROR FAILED &color CF ) command2 if %errorlevel% EQU 0 (echo OK ) Else ( Echo ERROR FAILED &color CF )

How to check the error level of an error?

The errorlevel is made available via IF ERRORLEVEL ... or the %ERRORLEVEL% variable. IF NOT ERRORLEVEL 3 means if ERRORLEVEL is less than 3 ( 2, 1, 0 or a negative number). To check for a specific error level N, you can use the following construct: IF ERRORLEVEL N IF NOT ERRORLEVEL N+1 COMMAND

How do I detect error levels in a batch file?

Detecting Errorlevels. There are two different methods of checking an errorlevel, the first syntax provides compatibility with old .bat batch files from the era of MS-DOS. The errorlevel is made available via IF ERRORLEVEL ... or the %ERRORLEVEL% variable. IF ERRORLEVEL n statements should be read as IF Errorlevel >= number.

Is errorlevel the same as%errorlevel% environment variable?

Raymond Chen [MSFT] explains: ERRORLEVEL is not the same as the %ERRORLEVEL% environment variable. In addition to setting an ERRORLEVEL, many utilities will output an error message on the error stream (STDERR), by default these messages will appear on the console, but they can be redirected with 2>.


2 Answers

You appear to have translated a batch script, which checks the ERRORLEVEL special variable in lieu or the more familiar to unix scripting exit code.

ERRORLEVEL is a special variable which corresponds to the ExitCode of a process.

What your code is attempting to do is to read an environment variable from it's own process as it was set by the child process.

In this case of batch scripts which appear to be doing this they are in fact relying on the the operating system's scripting environment to alter this special variable for you when the laucnhed process exits. This is problematic when trying to replicate a similar functionality within a fuller featured programming environment (for why this would be a bad idea from c# consider what would happen if two threads launched two processes at the same time).

Instead you have a decent API for interacting with the child process, the idiomatic equivalent is the following:

using (Process process = new Process())
{
    process.StartInfo = startInfo;
    process.Start();
    // only if you need the output for debugging
    //string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    return process.ExitCode;
}

Note here (as with the other answer) that you must first wait for the process to exit before you can look at the exit code.

like image 116
ShuggyCoUk Avatar answered Sep 28 '22 16:09

ShuggyCoUk


As far as I know that is just the ExitCode of your Process. Use that.

And it would only be useful to check that after waiting for the process to end, btw.

like image 36
Benjamin Podszun Avatar answered Sep 28 '22 17:09

Benjamin Podszun