I've been searching and experimenting all morning with this one and I'm stumped. I have an aspx page running in IIS and calling the following c# function. I'm trying to have it run a cmd file and return the output from the cmd file. I've experimented with the five different options in the code below:
protected String RunMyCmdFileAndGetResponse() {
Process proc = new Process ();
proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
// proc.StartInfo.Arguments = @"/c echo hello"; <== 1
// proc.StartInfo.Arguments = @"/c c:\mypath\myfile_badname.cmd"; <== 2
// proc.StartInfo.Arguments = @"/c type c:\mypath\myfile.cmd"; <== 3
proc.StartInfo.Arguments = @"/c c:\mypath\myfile.cmd"; // <== 4
// proc.StartInfo.Arguments = @"/c call c:\mypath\myfile.cmd"; <== 5
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
proc.WaitForExit();
return response;
}
Cmd file c:\mypath\myfile.cmd contents are:
@echo test line 1 > c:\mypath\myfilelog.txt
@echo test line 2
This cmd file works as expected when run manually, producing myfilelog.txt and returning test line 2. When executed with the c# code:
- Option 1 works - returns 'hello' response as expected.
- Option 2 fails as expected, indicating myfile_badname.cmd is not recognized as a valid command
- Option 3 works as expected - it returns the contents of myfile.cmd as the response - this confirms I am able to find and read the file.
- Option 4 does not work - as near as I can figure, it should. It does not hang up, but also does not return any response at all, and does not execute the cmd file (no myfilelog.txt produced).
- Option 5 - same results as option 4.
Note - I've also tried modifying myfile.cmd to remove line 1 (creating the log file) and only leave line 2 to echo a response. Just in case it's a permission issue creating the log file. Same result.
Any help would be appreciated!
Updated to add solution:
The answer from @MaxOvrdrv got me thinking a different way. There does indeed appear to be some kind of limitation when running Process.Start within an IIS context with UseShellExecute = false - if the primary argument is an executable file (cmd file, script file, etc), it will not run it. I tried passing SomeExample.cmd to cmd.exe, and SomeExample.js to cscript.exe.
However... I was able to trick it with a level of indirection, such that the executable file name is no longer the first argument, and it works just fine that way.
To run a cmd file:
string theResponse = RunMyCmdAndGetResponse(@"c:\somepath\mycmd.cmd");
protected String RunMyCmdAndGetResponse(string cmdPath) {
Process proc = new Process ();
proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cmd /c " + cmdPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}
To run a script file:
string theResponse = RunMyScriptAndGetResponse(@"c:\somepath\myscript.js");
protected String RunMyScriptAndGetResponse(string scriptPath) {
Process proc = new Process ();
proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cscript //nologo " + scriptPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}