-
January 30th, 2013, 11:32 AM
#1
Retrieve information pro command prompt
I have this block of code, which I'm using to enter a command and retrieve the result:
Code:
private void Cmd(string command, string parameter, object stream)
{
StreamWriter writer = (StreamWriter)stream;
StreamWriter input;
StreamReader output;
Process process = new Process();
try
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.FileName = "cmd";
process.Start();
input = process.StandardInput;
output = process.StandardOutput;
input.WriteLine("help");
while(!output.EndOfStream) //Locks when reach end of stream
{
writer.WriteLine(output.ReadLine());
Debug.WriteLine(output.ReadLine());
}
}
catch (Exception e)
{
InsertLog(2, "Could not run CMD command");
writer.WriteLine("Not valid. Ex: " + e.Message);
}
writer.Flush();
}
It works, except that the application locks when I reach the line highlighted on the code. On debug the yellow arrow simply disappear when reaches the while loop for the last time (end of stream).
-
January 30th, 2013, 12:03 PM
#2
Re: Retrieve information pro command prompt
Not sure but it sounds like it is waiting on input again. So your help text was displayed and now it is looking for maybe the "Exit command?
Try adding
input.WriteLine("exit");
After your loop.
-
January 30th, 2013, 12:08 PM
#3
Re: Retrieve information pro command prompt
Didn't work. Even tried to add WaitForExit(). Had to add it before the loop, now it works. Thank you.
Last edited by blacblu; January 30th, 2013 at 02:18 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|