2

I have tried to redirect the command prompt output to a file using Asp.Net C#.

System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = "c:\\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = @"/c dir" +">" + @"Myval.txt";
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
si.Start();
string output = si.StandardOutput.ReadToEnd();
Response.Write(output);
si.Close();

The file is getting created successfully but no content present in it. Even the variable Output returns nothing. Help me to resolve this issue.

Dineshkumar
  • 1,468
  • 4
  • 22
  • 49

3 Answers3

1

EDIT after being corrected:

I just tested on my machine and the code works perfectly. I apologize for not reading and testing carefully myself. Myval.txt is created and the DIR output is written into it.

The output variable is empty because you are rerouting any output by the DIR command into the txt file, so that's by design.

Please see if there are any locks on the txt file preventing it from being overwritten. Further than that, I can only guess that there is a security issue preventing the DIR command from running.

Alexander
  • 2,457
  • 1
  • 14
  • 17
  • On a side note, what are you trying to do? There's a huge chance you won't be able to call such commands in a hosted environment, due to security restrictions. – Alexander Jul 25 '13 at 07:08
  • I want to execute an exe at server lever in order to extract some value from the user input and gather the output in a file. – Dineshkumar Jul 25 '13 at 07:19
  • You will need at least a FullTrust hosting environment, and not all of those will allow you to run executables. – Alexander Jul 25 '13 at 07:21
  • Gonna try self hosting. Thanks for your help. – Dineshkumar Jul 25 '13 at 07:25
  • 2
    @Alexander, the syntax is fine. It works in a console app (commented out Response.Write). The output variable is empty because the command does not have any ouput, the output is saved to file. To display output and save to file his command should be: @"/c dir >Myval.txt && type Myval.txt". The /c switch means that cmd should run and then terminate. See http://ss64.com/nt/cmd.html for more info. But agreed on the security restrictions. – Riv Jul 25 '13 at 07:30
  • I'd rather delete my answer, your comment deserves the upvote, Reubz. I'm not sure about the etiquette here on SO. Is it okay to delete? – Alexander Jul 25 '13 at 07:43
  • Don't delete your answer, just remove your first paragraph. Your edit explains it fine. – Riv Jul 25 '13 at 09:03
0

IIS7 - I tested this various ways including using a Batch file but the application isn't available on desktop. I can see the worker process and the exe running under my user name but with session id value of zero.

0

The following has worked for me through command prompt:

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "YOURBATCHFILE.bat";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();
codebased
  • 6,945
  • 9
  • 50
  • 84