Get output of existing process [duplicate] - c#

This question already has answers here:
C#: Redirect Standard Output of a Process that is Already Running
(4 answers)
Closed 8 years ago.
Can I get output of existing proccess? When I'm trying it says process is not running or doesn't have output redirect. Example I launched ping google.com -t (waited 20 seconds to have ouput) and I want last 10 lines from it:
var processes = Process.GetProcessesByName("PING").FirstOrDefault();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
process.BeginOutputReadLine();
Any ideas how I can get it?

Try:
process.StartInfo.UseShellExecute = false;
process.Start();
process.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
process.BeginOutputReadLine();

Related

C# Start Program with ADMIN rights from other program [duplicate]

This question already has answers here:
Elevating privileges doesn't work with UseShellExecute=false
(3 answers)
Closed 1 year ago.
I´ve written a notify program, which communicats with an windows service on WebSocket. This notify program does not have admin rights but with click on button starts an other program which starts and stops the service. This program should run with admin rights, it worked very fine for some time but somehow not any more.
Thats my code snippet:
using (var process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}
The exception is the following:
System.ComponentModel.Win32Exception: "Der angeforderte Vorgang erfordert erhöhte Rechte"
Back then it showed a window where I could accept admin rights but does not any more.
Try adding this line
process.StartInfo.UseShellExecute = true;
on
using (var process = new Process())
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}

StreamReader.ReadToEnd() returns empty string [duplicate]

This question already has answers here:
Process.start: how to get the output?
(11 answers)
ProcessStartInfo hanging on "WaitForExit"? Why?
(22 answers)
Closed 4 years ago.
I am trying to run a command in cmd.exe, and redirect the output to a textfile. I have verified that the command is being executed, but when I call StandardOutput.ReadToEnd() or StandardError.ReadToEnd(), an empty string is returned instead of the text output from the command. Am I missing something?
ProcessStartInfo PSI = new ProcessStartInfo("cmd.exe", command);
PSI.UseShellExecute = false;
PSI.CreateNoWindow = true;
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.Arguments = "/c";
var proc = Process.Start(PSI);
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string errors = proc.StandardError.ReadToEnd();
Console.WriteLine(errors);
I'm pretty sure using ReadToEnd doesn't work if you're also capturing error output at the same time. You'll need to use proc.BeginOutputReadLine() instead (and proc.BeginErrorReadLine() for the error output).
However, those methods are asynchronous, so you'll need to use event handlers to actually get the output.
PSI.EnableRaisingEvents = true;
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(OutputReceivedHandler);
proc.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(ErrorReceivedHandler);
The handlers have the output/error data stored in the event argument's Data property.
private void OutputReceivedHandler(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
private void ErrorReceivedHandler(object sender, ErrorReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
Since this is all asynchronous, you'll want to ditch the WaitForExit call, as that will block unnecessarily. If you do want the call to block, you can use WaitForExit, but refer to the answer that user Greg linked in the comments for an implementation that won't result in a buffer overflow.

How I can make to appear a error message if the user stopping brutal the process? [duplicate]

This question already has answers here:
Process Exit Code When Process is Killed Forcibly
(2 answers)
Closed 7 years ago.
I have a method to install a program:
private void Install_Click(object sender, EventArgs e)
{
using (Process process = new Process())
{
// The installer itself
process.StartInfo.FileName = ExeFile;
if (fileArr1.Equals("installer.ini"))
process.StartInfo.Arguments = #"-if C:\temp\installer.ini";
if (fileArr1.Equals("installer_input.txt"))
process.StartInfo.Arguments = #"-if C:\temp\installer_input.txt";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
}
}
How I can make to appear a error message : " the process was stop " if the user stopping brutal the process?
If your process has a name, you could check this doing the folowing:
if (!Process.GetProcessesByName("process_name").Length > 0)
{
// Is not running
}
use BackgroundWorker and waitfrom, so user won't stop process

Run Unix commands using PuTTY in C# [duplicate]

This question already has answers here:
Automating running command on Linux from Windows using PuTTY
(9 answers)
Closed 8 years ago.
I am trying to run Unix commands in PuTTY using C#. I have the below code. But the code is not working. I am not able to open PuTTY.
static void Main(string[] args)
{
Process cmd = new Process();
cmd.StartInfo.FileName = #"C:\Windows\System32\cmd";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = false;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.Start();
cmd.StartInfo.Arguments = "C:\Users\win7\Desktop\putty.exe -ssh mahi#192.168.37.129 22 -pw mahi";
}
First, in general, you better use native .NET SSH library, like SSH.NET, instead of running external application.
See How to run commands on SSH server in C#?
The putty.exe is a GUI application. It's intended to interactive use, not for automation. There's no point trying to redirect its standard output, as it's not using it.
For automation, use another tool from PuTTY package, the plink.exe.
It's a console application, so you can redirect its standard output/input.
There's no point trying to execute an application indirectly via the cmd.exe. Execute it directly.
You need to redirect standard input too, to be able to feed commands to the Plink.
You have to provide arguments before calling the .Start().
You may want to redirect error output too (the RedirectStandardError). Though note that you will need to read output and error output in parallel, what complicates the code.
static void Main(string[] args)
{
Process cmd = new Process();
cmd.StartInfo.FileName = #"C:\Program Files (x86)\PuTTY\plink.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.Arguments = "-ssh mahi#192.168.37.129 22 -pw mahi";
cmd.Start();
cmd.StandardInput.WriteLine("./myscript.sh");
cmd.StandardInput.WriteLine("exit");
string output = cmd.StandardOutput.ReadToEnd();
}
This should work:
static void Main(string[] args)
{
ProcessStartInfo cmd = new ProcessStartInfo();
cmd.FileName = #"C:\Users\win7\Desktop\putty.exe";
cmd.UseShellExecute = false;
cmd.RedirectStandardInput = false;
cmd.RedirectStandardOutput = true;
cmd.Arguments = "-ssh mahi#192.168.37.129 22 -pw mahi";
using (Process process = Process.Start(cmd))
{
process.WaitForExit();
}
}

C# Show output of Process in real time [duplicate]

This question already has answers here:
Redirect console output to textbox in separate program
(4 answers)
C# - Capturing Windows Application Output
(3 answers)
Closed 9 years ago.
In C# I am starting a 3rd party application that takes 2 - 3 hours to complete. I need the output of the Process to write to the console in real time. I have done research on BeginOutputReadLine() and RedirectStandardOutput from Microsoft's website but my code is still not working.
Currently my code is only showing the output when the process is finished. I don't know where its gone wrong.
static void Main(string[] args)
{
Process process;
process = new Process();
process.StartInfo.FileName = "C:\\ffmbc\\ffmbc.exe";
process.StartInfo.Arguments = "-i \\\\dssp-isi-t\\TMD\\B002C010_130520_R2R7.2398v5.mxf -an -vcodec libx264 -level 4.1 -preset veryslow -tune film -x264opts bluray-compat=1:weightp=0:bframes=3:nal-hrd=vbr:vbv-maxrate=40000:vbv-bufsize=30000:keyint=24:b-pyramid=strict:slices=4:aud=1:colorprim=bt709:transfer=bt709:colormatrix=bt709:sar=1/1:ref=4 -b 30M -bt 30M -threads 0 -pass 1 -y \\\\dss-isi-t\\MTPO_Transfer\\dbay\\B002C010_130520_R2R7.2398v5.mxf.h264";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.Close();
}
private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
string line;
line = (outLine.Data.ToString());
Console.WriteLine(line);
}
Similar to a previous question I'd answered, maybe even a duplicate.
See: Pipe a stream to Debug.Write()
Here's my answer (modified slightly) from that:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += p_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();
Then, your event handler for receiving data.
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.Write(e.Data);
}
Basically, you just need to nix the WaitForExit(), since that makes your program hang until the process completes.
The line
process.WaitForExit();
will cause the current program to wait until the given process finishes. This is most certainly not what you want; you probably want to start the process, let it run asynchronously, and then let it tell you when it finishes. For that, you will want to use the process.Exited event.

Categories

Resources