i am trying the below code
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = new StreamWriter(p.StandardInput.BaseStream))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(#"dir");
}
}
using (StreamWriter sw = new StreamWriter(p.StandardInput.BaseStream))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(#"dir");
}
}
the first sw.writeline is going ok, but the second i discovered that the process hasexeited is true.
why the process exited , and how to keep it running to execute further commands?
Put both write commands in the same using block and omit the second using block. The using-block disposes the StreamWriter after you leave the block. Disposing the StreamWriter closes the underlying stream and causes cmd to exit.
i solved the problem like that
static Queue<string> CmdQueue = new Queue<string>();
static Thread ThreadCMD = new Thread(new ThreadStart(CMDThread));
private static void CMDThread()
{
bool Active = true; // used to indicate the status of the session
ProcessStartInfo psiOpt = new ProcessStartInfo();
psiOpt.FileName = "cmd.exe";
psiOpt.WindowStyle = ProcessWindowStyle.Hidden;
psiOpt.RedirectStandardOutput = true;
psiOpt.RedirectStandardInput = true;
psiOpt.RedirectStandardError = true;
psiOpt.UseShellExecute = false;
psiOpt.CreateNoWindow = true;
Process procCommand = Process.Start(psiOpt);
procCommand.OutputDataReceived += ProcCommand_OutputDataReceived;
procCommand.ErrorDataReceived += ProcCommand_ErrorDataReceived;
procCommand.BeginOutputReadLine();
procCommand.BeginErrorReadLine();
using (StreamWriter sw = procCommand.StandardInput)
{
while (Active)
{
while (CmdQueue.Count==0) { }
string cmd = CmdQueue.Dequeue();
if (cmd != "cmdexit")
{
sw.WriteLine(cmd);
}
else
{
Active = false;
}
}
}
procCommand.OutputDataReceived -= ProcCommand_OutputDataReceived;
procCommand.ErrorDataReceived -= ProcCommand_ErrorDataReceived;
procCommand.Close();
}
private static void ProcCommand_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
PushCMDResult(Encoding.ASCII.GetBytes(e.Data));
}
private static void ProcCommand_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
PushCMDResult(Encoding.ASCII.GetBytes(e.Data));
}
public void StartCMD()
{
ThreadCMD.Start();
}
public void Execute(string cmd)
{
CmdQueue.Enqueue(cmd);
}
i can start cmd.exe session , and execute as many as needed of commands using execute to enqueue the cmd string in Queue which will be written to the streamwriter .
i used async reading for output and error as well.
Related
I have been looking for a solution before posting but I gave up!
I just want to interactively send DOS command using Standard input. It works well but I always don't get the last line (the prompt line) on the OutputDataReceived callback.
Any ideas?
Process p = null;
private void Start_Click(object sender, EventArgs e)
{
p = new Process();
p.StartInfo = new ProcessStartInfo();
p.EnableRaisingEvents = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "cmd.exe";
p.ErrorDataReceived += ErrorDataReceived;
p.OutputDataReceived += OutputDataReceived;
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.StandardInput.WriteLine("dir");
}
private void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data + "\n");
}
private void WriteToStandardInput_Click(object sender, EventArgs e)
{
p.StandardInput.WriteLine(txt_command.Text); //can be "dir" or "cd temp"
}
Adding p.StandardInput.Close() solves the problem, reason is when you close the input stream it actually terminates the process (which you start using 'p.start'). So as I said, you need start separate process for each command.
~Nilesh
OK, I have found a solution... I have created 2 tasks as shown below which are reading constantly from the output and error stream and print it to a rich text box. The trick was not to use BeginErrorReadLine and BeginOutputReadLine.
I hope I was able to help others...
public partial class Form1 : Form
{
private Process p = new Process();
private SynchronizationContext context;
public Form1()
{
InitializeComponent();
context = SynchronizationContext.Current;
}
private void Start_Click (object sender, EventArgs e)
{
p.StartInfo = new ProcessStartInfo();
p.EnableRaisingEvents = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.Start();
Task.Run(() => ReadFromStreamLoop(p.StandardOutput));
Task.Run(() => ReadFromStreamLoop(p.StandardError));
}
private void ReadFromStreamLoop (StreamReader s)
{
int count = 0;
char[] buffer = new char[1024];
do
{
StringBuilder builder = new StringBuilder();
count = s.Read(buffer, 0, 1024);
builder.Append(buffer, 0, count);
context.Post(new SendOrPostCallback(delegate (object state)
{
richTextBox1.AppendText(builder.ToString());
}), null);
} while (count > 0);
}
private void WriteToStandardInput_Click (object sender, EventArgs e)
{
p.StandardInput.WriteLine(txt_command.Text); //can be "dir" or "cd temp"
}
}
If you are looking for event end of command execution, then every time spawn a new process. At the end of processing you can show the prompt on main process. Manage your environment variable through invoking(main) process.
See the example on - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.outputdatareceived?view=netframework-4.7.2
I want to perform some command lines to display the result after each input.
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("ftp");
//output
sw.WriteLine("open ftp.server.com");
//output
sw.WriteLine("username");
//output
sw.WriteLine("password");
//output
}
}
Help me to understand how to make the output result after each sw.WriteLine(...)?
Updated
It is not working with ftp. Why?
Initialization:
Test test = new Test();
test.start();
Console.ReadKey();
Class Test:
class Test
{
static StringBuilder StdOutput = new StringBuilder();
Process p = null;
Queue<string> cmdQueue = new Queue<string>();
public void start(){
cmdQueue = new Queue<string>();
cmdQueue.Enqueue("cd c:\\");
cmdQueue.Enqueue("dir");
cmdQueue.Enqueue("ftp");
cmdQueue.Enqueue("open us1.hostedftp.com");
cmdQueue.Enqueue("z3r9#ya.ru");
cmdQueue.Enqueue("123456");
cmdQueue.Enqueue("dir");
setupProcess();
startProcess();
}
private void setupProcess()
{
p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd";
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.OutputDataReceived += new DataReceivedEventHandler(OutputDataHandler);
StdOutput = new StringBuilder();
p.StartInfo = info;
}
private async void startProcess()
{
p.Start();
p.BeginOutputReadLine();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
while (cmdQueue.Count > 0)
{
string cmd = cmdQueue.Dequeue();
if (cmd != null & cmd != "")
{
await sw.WriteLineAsync(cmd);
Thread.Sleep(100);
//System.Console.WriteLine(StdOutput);
}
else
{
break;
}
}
Console.WriteLine(StdOutput);
}
p.WaitForExit();
}
}
private static void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
StdOutput.Append(Environment.NewLine + outLine.Data);
//System.Console.WriteLine(Environment.NewLine + outLine.Data);
}
}
}
I assume that you are actually asking about how to catch outputs from all the commands you want to have executed in the (one) process.
Here is a version of a solution I came up with a long time ago, when I was a rookie here..
The trick is to collect the output as is comes along by listening to events the Process will trigger whenever output gets created: OutputDataReceived and ErrorDataReceived. We need to run things async for this to work, so it will look a little more complicated than the usual examples, which only have one process executing one command..:
First a few variables:
Queue<string> cmdQueue = new Queue<string>();
static StringBuilder StdOutput = new StringBuilder();
static StringBuilder ErrOutput = new StringBuilder();
Process p = null;
Task processTask = null;
bool processIsRunning = false;
Here is a button click event that starts processing all commands from a multiline TextBox. Output gets collected in the two StringBuilders; when the queue is empty, I wait a little longer..:
private void button1_Click(object sender, EventArgs e)
{
cmdQueue = new Queue<string>(tb_commands.Lines.ToList());
setupProcess();
startProcessTask();
while (cmdQueue.Count > 0) Thread.Sleep(100);
Thread.Sleep(500);
tb_out.AppendText(StdOutput + "\r\n" + ErrOutput + "\r\n");
}
Here is the routine that set up the Process. Here we register two events that will notify us when there are lines in the output streams..:
private void setupProcess()
{
p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.OutputDataReceived += new DataReceivedEventHandler(OutputDataHandler);
p.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataHandler);
StdOutput = new StringBuilder();
ErrOutput = new StringBuilder();
p.StartInfo = info;
}
After the setup we can start a Task that will start our Process asynchonously..:
private void startProcessTask()
{
var task = Task.Factory.StartNew(() => startProcess());
processTask = task;
}
..and finally here is the async method that after starting the Process and beginning with the asynchronous read operations on the redirected streams, keeps feeding it all lines from the command queue.
private async void startProcess()
{
try { p.Start(); processIsRunning = true; } catch
{
ErrOutput.Append("\r\nError starting cmd process.");
processIsRunning = false;
}
p.BeginOutputReadLine();
p.BeginErrorReadLine();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
do
{
try
{
string cmd = cmdQueue.Dequeue();
if (cmd != null & cmd != "") await sw.WriteLineAsync(cmd);
} catch { }
} while (processIsRunning);
try { p.WaitForExit(); } catch { ErrOutput.Append("WaitForExit Error.\r\n"); }
}
}
The last pieces are the two events we have registered for reading the output from the two streams and adding them to the StringBuilders:
private static void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
StdOutput.Append(Environment.NewLine + outLine.Data);
}
}
private static void ErrorDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
ErrOutput.Append(Environment.NewLine + outLine.Data);
}
}
Note that this works fine for all sorts of commands you can feed into the process, including FTP. Here I change my codepage, show the images I have before, log in to an FTP server, call up the help page, cd and dir, download an image, close the connection and check the images I have now..:
One Caveat: There must be something wrong in the way I wrote this, as VS keeps complaining about a System.InvalidOperationException and the exe file hogs ~10% cpu. Helping me out would be very much appreciated..
I need to run PLink as a process as part of WinForm application
this is my code
public void RunProcess(string FileName, string Arguments, bool EventWhenExit , bool IsWaitBeforeStart = true )
{
process = new Process();
process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);//**
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = FileName; // Gets or sets the application or document to start.
process.StartInfo.Arguments = Arguments;//Gets or sets the set of command-line arguments to use when starting the application
if (IsWaitBeforeStart) Thread.Sleep(5000);
if (EventWhenExit)
{
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(myprocess_Exited);
}
process.Start();
process.BeginOutputReadLine();
PID = process.Id;
ProcessTimeOut.Enabled = true;
ProcessInputStream = process.StandardInput;
ProcessTimeOut.Enabled = false;
}
private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e)
{
//prints to screen using control.invoke
//add data to a string list
}
My setup consist of a telnet server that I need to run few command on it and parse the result
if I run the application from cmd it prints the result under one sec (it is about 50 rows)
but if I run it using my code it takes up to almost 7 sec !
From my understanding process.start() and running via cmd should be the same
so the problem should be somewhere in my code or logic
what can be the problem ?
Ok so with help from Vajura comment I've made a simple(vary) buffer to implement simple consumer/producer pattern
inside RunProcess :
public void RunProcess(string FileName, string Arguments, bool EventWhenExit , bool IsWaitBeforeStart = true )
{
//... same code as before
PollingService();
}
second changing the event DataReceivedEventHandler
to store data to buffer (and stop invoking the print to UI)
the code is something like ProcessLog.Add(e.Data);
now for the second Thread to run over the buffer :
private void PollingService()
{
var T = new Thread (()=>
{
while (true)
{
if (ProcessLogIndex < ProcessLog.Count)
{
lock (this)
{
var tempList = ProcessLog.GetRange(ProcessLogIndex, ProcessLog.Count - ProcessLogIndex);
ProcessLogIndex = ProcessLog.Count;
foreach (var cell in tempList)
{
string ToSend = !string.IsNullOrEmpty(cell) ? (cell.Contains('$') ? cell.Substring(cell.LastIndexOf('$')) : cell) : "";
onDataOutputFromProcess(this, ToSend, Proc.ToString());
}
}
}
Thread.Sleep(1000);
}
});
T.IsBackground = true;
T.Start();
}
I have a windows service that runs under a logon say "UserA". The job of the windows service is to start a new process on a timer elapsed event.
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Task.Factory.StartNew(() => StartNewProcess());
}
private void Initialize()
{
newProcess = new Process();
newProcess.StartInfo.FileName = "Test.exe";
newProcess.StartInfo.Arguments = "sessionId...";
newProcess.StartInfo.CreateNoWindow = false;
newProcess.StartInfo.ErrorDialog = false;
newProcess.StartInfo.RedirectStandardError = true;
newProcess.StartInfo.RedirectStandardInput = true;
newProcess.StartInfo.UseShellExecute = false;
newProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
private void StartNewProcess()
{
newProcess.Start();
}
On task manager I see that both the windows service and the new process have a "User Name" as "UserA".
But the problem is Windows service is able to access "C:\QueryResult" but the new process is not able to access "C:\QueryResult"
I am using File.Copy("C:\QueryResult", "D:\blahblah") on both the process
Has the security context changed in the new process?
Try automatically elevating the application permissions like so:
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;
public static class Program
{
[STAThread]
public static void Main()
{
if (!IsAdmin() && IsWindowsVistaOrHigher())
RestartElevated();
else
DoStuff();
}
private static Boolean IsAdmin()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
if (identity != null)
return (new WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);
return false;
}
private static Boolean IsWindowsVistaOrHigher()
{
OperatingSystem os = Environment.OSVersion;
return ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 6));
}
private static void RestartElevated()
{
String[] argumentsArray = Environment.GetCommandLineArgs();
String argumentsLine = String.Empty;
for (Int32 i = 1; i < argumentsArray.Length; ++i)
argumentsLine += "\"" + argumentsArray[i] + "\" ";
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = argumentsLine.TrimEnd();
info.CreateNoWindow = false;
info.ErrorDialog = false;
info.FileName = Application.ExecutablePath;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.Verb = "runas";
info.WindowStyle = ProcessWindowStyle.Hidden;
info.WorkingDirectory = Environment.CurrentDirectory;
try
{
Process.Start(info);
}
catch { return; }
Application.Exit();
}
}
I am reading the output of a java application started using Process and reading stdError, stdOutout and using stdInput to send commands. Here is the relevant code:
int mem = Properties.Settings.Default.mem_max;
string locale = Properties.Settings.Default.location;
Process bukkit_jva = new Process();
bukkit_jva.StartInfo.FileName = "java";
//bukkit_jva.StartInfo.Arguments = "-Xmx" + mem + "M -Xms" + mem + "M -jar " + locale + "bukkit.jar";
bukkit_jva.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\bukkit\\bukkit.jar";
bukkit_jva.StartInfo.UseShellExecute = false;
bukkit_jva.StartInfo.CreateNoWindow = true;
bukkit_jva.StartInfo.RedirectStandardError = true;
bukkit_jva.StartInfo.RedirectStandardOutput = true;
bukkit_jva.StartInfo.RedirectStandardInput = true;
bukkit_jva.Start();
//start reading output
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
StreamReader err = bukkit_jva.StandardError;
StreamReader output = bukkit_jva.StandardOutput;
StreamWriter writer = bukkit_jva.StandardInput;
SetText(err.Peek().ToString());
while (false == false)
{
if (vars.input != null)
{
writer.WriteLine(vars.input);
vars.input = null;
}
SetText(output.ReadLine() + err.ReadLine());
}
}
SetText() adds the line to a list of lines.
My problem is that the java app sometimes returns a string even when there is no input, so I always need to check for a new line. but If I need to send a command, and there is no new output, It will not send.
I tried different If statements on the readline, but it would only return the first few lines then it would stop.
basicly it seems to pause the loop if there is no new line for it to read.
How could I either setup my read/write loop differently or get the loop to unpause?
Thanks,
Adam
try this:
static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo("echoApp.exe");
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process echoApp = new Process();
echoApp.ErrorDataReceived += new DataReceivedEventHandler(echoApp_ErrorDataReceived);
echoApp.OutputDataReceived += new DataReceivedEventHandler(echoApp_OutputDataReceived);
echoApp.StartInfo = psi;
echoApp.Start();
echoApp.BeginOutputReadLine();
echoApp.BeginErrorReadLine();
echoApp.StandardInput.AutoFlush = true;
string str = "";
while (str != "end")
{
str = Console.ReadLine();
echoApp.StandardInput.WriteLine(str);
}
echoApp.CancelOutputRead();
echoApp.CancelErrorRead();
echoApp.Close();
}
static void echoApp_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("stdout: {0}", e.Data);
}
static void echoApp_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("stderr: {0}", e.Data);
}
and the little echoApp...
//echoApp
static void Main(string[] args)
{
string str="";
while (str != "end")
{
str = Console.ReadLine();
Console.WriteLine(str);
}
}
Please note if you try to access the output after CancelOutputRead() and CancelErrorRead() you may find that occasionally you are missing some text. I found the flush only occurs after explicitly calling Close(). Disposing (with a using statement) doesn't help.
This symptom will most likely occur when calling the command processor (CMD.EXE) because it does not explicitly flush itself. So be careful not to try and access the output (written from your event handlers) unless you explicitly call Close() first.