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?
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 )
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
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.
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>.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With