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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
You will need at least a FullTrust hosting environment, and not all of those will allow you to run executables.
Gonna try self hosting. Thanks for your help.
@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 ss64.com/nt/cmd.html for more info. But agreed on the security restrictions.
|
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.

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.