I have a C# code that uses command prompt to call a python script. The script currently takes about 25 seconds to run. When run in command prompt, the python script has several outputs until finally outputting "done". My C# code runs the script, but never closes the command prompt if I use "WaitForExit". So my thought is I need some kind of logic to check to see if "done" has been outputted, but the internet has not been very helpful with methodology.
Here's what I have, it currently only outputs the first line, "Microsoft Windows [Version 6.1.7601]". Obviously, no good.
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"C:\Optimization";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(#"ipy spot_for_labview.py");
StreamReader mySR = p.StandardOutput;
string mystr = mySR.ReadLine();
Debug.WriteLine(mystr);
p.WaitForExit(25000); //25000 is a placeholder until better method found.
p.Close();
If there's anyway to close the process after it finishes, or to get all the cmd output I'm all ears.
Any help is appreciated.
did you try this event ? Process.OutputDataReceived Event
or Process.ErrorDataReceived Event
here is the code from MSDN
Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
...
....
...
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
}
Related
I tried lot's of samples posted here and elsewhere that should capture and output the StandardOutput of a process.start command. But no matter if synchronous or asynchronous the output is always empty.
StringBuilder outputBuilder;
ProcessStartInfo processStartInfo;
Process process;
outputBuilder = new StringBuilder();
processStartInfo = new ProcessStartInfo();
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.UseShellExecute = false;
processStartInfo.Arguments = "-ss 00:00:50 -t 240 -i Input.MOV -to 00:00:02 -qscale 0 _OUTPUT.MOV";
processStartInfo.FileName = "ffmpeg.exe";
process = new Process();
process.StartInfo = processStartInfo;
// enable raising events because Process does not raise events by default
process.EnableRaisingEvents = true;
// attach the event handler for OutputDataReceived before starting the process
process.OutputDataReceived += new DataReceivedEventHandler
(
delegate(object sender, DataReceivedEventArgs e)
{
// append the new data to the data already read-in
outputBuilder.Append(e.Data);
}
);
// start the process
// then begin asynchronously reading the output
// then wait for the process to exit
// then cancel asynchronously reading the output
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();
// use the output
string output = outputBuilder.ToString();
Process is executed just as intended but there is not output captured nevertheless it's shown in the VS IDE output pane.
Any idea whats going wrong here?
Best Regards.
Thanks Amy, it's correct, the normal output is returned by
BeginErrorReadLine
In C#, I am trying to run a command line program that gets input from stdin and then it returns the output to stdout . I need to keep cmd.exe running (using /k) and in a for loop, send in text and then wait for the output before sending the next text. This works if I don't redirect stdout but not after I redirect it. Initially I got data back from stdout (although much later) and now that is no longer working other than the initial call to start the program. This link says "Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread."
Would that fix my issue and if so, how would I do that?
The code is set up as follows:
StringBuilde sb = new StringBuilder();
Process process = new Process();
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = workingdirectory;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/k";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += (sender, args) => AppendData(args.Data); //this appends the output to a stringbuilder
process.ErrorDataReceived += (sender, args) => AppendError(args.Data);
process.Start();
//sw is a streamwriter
sw= process.StandardInput;
//now call the command line code
sw.WriteLine(" some.exe some.arg ");
process.BeginOutputReadLine();
foreach(DataRow row in dtMydata.Rows)
{
mytext=row["Text"].toString();
sw.WriteLine(mytext);
sw.WriteLine(Environment.NewLine); //This redirects the text to the program,
}
I am working on a project of remotely receiving commands from a server, but I am facing a problem when working with the command prompt locally. Once I get it working locally, then I will move to remote communication.
Problem:
I have to completely hide the console, and client must not see any response when the client is working with the command line but it will show a console for a instance and then hide it.
I had to use c# to send a command to cmd.exe and receive the result back in C#. I have done it in one way by setting the StandardOutput... and input to true.
The commands are not working. For example, D: should change the directory to D and it does, but after that, if we use dir to see the directories in D, it does not show the appropriate directories.
Here is my code:
First Method
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + textBoxInputCommand.Text + " >> " + " system";
process.StartInfo = startInfo;
process.Start();
Second Method
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + textBoxInputCommand.Text);
procStartInfo.WorkingDirectory = #"c:\";
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = true;
procStartInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
richTextBoxCommandOutput.Text += result;
I want the program to run as administrator because the exe it generates does not run commands when it runs from the C drive.
Try not to run the commands by passing them to cmd instead write the commands passed by the client to a.bat file execute the .bat. file from your program this will probably hide your command prompt window.
You can also use process.OutputDataRecieved event handler to do anything with the output.
If you want to execute command using administrator rights you can use runascommand. It is equivalent to the sudo command in Linux. Here is a piece of code may be it will help you
var process = new Process();
var startinfo = new ProcessStartInfo(#"c:\users\Shashwat\Desktop\test.bat");
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
process.StartInfo = startinfo;
process.OutputDataRecieved += DoSomething;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
//Event Handler
public void DoSomething(object sener, DataReceivedEventArgs args)
{
//Do something
}
Hope it helps you.
You could hide command prompt window by adding this line of code:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
or do not create it at all
startInfo.CreateNoWindow = true;
Here can be found a few awarding solutions:
Run Command Prompt Commands
I am trying to run a python code from my UI. i am using the code below to do so,
Process p = new Process();
string cmd = #"python filepath & exit";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
StreamWriter myStreamWriter = p.StandardInput;
myStreamWriter.WriteLine(cmd.ToString());
myStreamWriter.Close();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
the above code opens a cmd prompt and executes the python file.
this code runs fine when i test it using a console application but it doesn't work when i use it within the event function for "run" button in my UI application. Is there any particular reason for it?
Obviously I can execute something with cmd console using Process.Start();
Is there any way to get output of that process? For example, I could have something like...
Process.Start("sample.bat");
... in my C# winforms application and sample.bat will contain something like:
echo sample loaded
First Question: is there any way to retrieve that sample loaded, after bat execution?
Second question: is there a way to use it without popped up console window?
There is an example of exactly how to do this in the Process documentation:
// 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 = "Write500Lines.exe";
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();
Yes, you can use
Process.Start(ProcessStartInfo)
There are a few ways to hook into I/O including ProcessStartInfo.RedirectStandardOutput available. You can use these overloads to read output from your batch files. You can also hook into the Exited event to know when execution is complete.
Use CreateNoWindow for no window.
Set process.StartInfo.RedirectStandardOutput to true and subscribe to process.OutputDataReceived
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo("exename");
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (s, ev) =>
{
string output = ev.Data;
};
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}