Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# run batch file, command not recognised

Tags:

c#

file

I have implemeneted a method into my c# program to run a batch file, which runs a virus scan on any files uploaded;

public static Int32 ExecuteCommand(String filePath, Int32 Timeout){
    Int32 ExitCode;
    ProcessStartInfo ProcessInfo = new ProcessStartInfo();
    ProcessInfo.CreateNoWindow = true;
    ProcessInfo.UseShellExecute = true;
    ProcessInfo.FileName = filePath;
    Process proc = Process.Start(ProcessInfo);
    proc.WaitForExit(Timeout);
    ExitCode = proc.ExitCode;
    proc.Close();
    return ExitCode;
 }

Ok now my batch file;

@ECHO OFF
c:
cd "..\AVG\AVG9\"
avgscana.exe /SCAN="..\learninglounge.com.solar.quarantine\" /REPORT="..\learninglounge.com.solar.antivirus\virusReports\report.txt"

EDIT : I do have fully qualified link to avg exe and directories but have replaced here with .. for purposes of posting to stackoverflow. Sorry if this caused confusion.

So my problem is the reporting side of my batch file. I can double click the batch file and it scans and creates the report no problem. When i run it through my c# i get an exit code of 2; command not recognised. It's fine if i remove the report part of my batch file. Now obviously this points to write permissions but I have checked that and the impersonated user has write access on the directory. Is there anything Im missing?

Thanks all

like image 801
Shannow Avatar asked May 18 '26 00:05

Shannow


1 Answers

The error states that avgscana.exe isn't located in directory which is set as "current" when you execute command. When you click on your bat file in Windows Explorer current directory is set to directory where bat file is located. Probably your avgscana.exe is located in the same folder so it works fine.

When you execute the command from .Net application current directory remains the same (if you haven't changed it then it will be a folder where .Net app is located). If your .Net app is located not in the same folder as bat file then you will get an error which you're actually getting. You should either specify a full path in your bat file or set Environment.CurrentDirectory in .Net app before launching bat.

like image 97
Snowbear Avatar answered May 19 '26 13:05

Snowbear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!