Running hg command in c# - c#

I have create a C# application for make easy of committing changes to mercurial repository. i have use following code to run hg commands in c# process
static void ExecuteCMDCommand(string path, string command)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.CreateNoWindow = false;
startInfo.WorkingDirectory = path;
startInfo.UseShellExecute = false;
startInfo.FileName = "hg.exe";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = command;
Process myProcess = new Process();
try
{
myProcess.StartInfo = startInfo;
var sbOut = new StringBuilder();
var sbErr = new StringBuilder();
var sw = new Stopwatch();
sw.Start();
myProcess.Start();
TimeSpan firstRead = TimeSpan.Zero;
myProcess.OutputDataReceived += (s, e) =>
{
if (firstRead == TimeSpan.Zero)
{
firstRead = sw.Elapsed;
}
sbOut.Append(e.Data);
};
Console.WriteLine("**************" + sbOut.ToString());
myProcess.ErrorDataReceived += (s, e) => sbErr.Append(e.Data);
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine();
var eventsStarted = sw.Elapsed;
myProcess.WaitForExit();
var processExited = sw.Elapsed;
sw.Reset();
// string strOutput = myProcess.StandardOutput.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally {
myProcess.Close();
myProcess.Dispose();
}
}
this works for hg status/hg branches but when i execute hg pull it is not working, i only see pulling from http://abc.abc.com/hg/repo.
What should i do for this?

Related

using docker programmatically and capturing the standardoutput

I would like to run some docker commands programmatically inside a process, and capture the standard output of docker. The commands do run without problems, but I cannot capture the standard output. For example, consider the following code. I would like to the read the docker info command, but all I get is a null string. Do you if there is a way to interact with docker in this way? Thanks
private static void VerifyDocker()
{
var processStartInfo = new ProcessStartInfo();
var messagesBuilder = new StringBuilder();
var arguments = "info --format '{{json .}}'";
using (var process = new Process())
{
processStartInfo.FileName = "docker";
processStartInfo.Arguments = arguments;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, e) => messagesBuilder.Append(e.Data);
process.StartInfo = processStartInfo;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();
var message = messagesBuilder.ToString();
}
}
Yes it's a good idea to capture both, and also use double quotes instead of single quotes for the parameter passed to --format.
private static void VerifyDocker()
{
var processStartInfo = new ProcessStartInfo();
var messagesBuilder = new StringBuilder();
var errorMessagesBuilder = new StringBuilder();
var arguments = "info --format \"{{json .}}\"";
using (var process = new Process())
{
processStartInfo.FileName = "docker";
processStartInfo.Arguments = arguments;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
process.OutputDataReceived += (sender, e) => messagesBuilder.Append(e.Data);
process.ErrorDataReceived += (sender, e) => errorMessagesBuilder.Append(e.Data);
process.StartInfo = processStartInfo;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.CancelOutputRead();
process.CancelErrorRead();
var message = messagesBuilder.ToString();
var errrors = errorMessagesBuilder.ToString();
}
}

c# Process output unreadable pckimmo

I have been trying to read process output for parsing morphological analysis. But I can't read pckimmo32.exe output.
public static string Problem1()
{
ProcessStartInfo _startInfo = new ProcessStartInfo();
Process p = new Process();
StringBuilder outputStringBuilder = new StringBuilder();
string filePath = AppDomain.CurrentDomain.BaseDirectory + #"..\..\PC-KIMMO\pckimmo32.exe";
var file = new FileInfo(filePath);
p.StartInfo = _startInfo;
_startInfo.UseShellExecute = false;
_startInfo.RedirectStandardOutput = true;
_startInfo.RedirectStandardInput = true;
_startInfo.WorkingDirectory = file.Directory.FullName;
_startInfo.FileName = file.FullName;
p.OutputDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data);
p.Start();
p.BeginOutputReadLine();
var myWriter = p.StandardInput;
myWriter.AutoFlush = true;
myWriter.WriteLine("synthesize kitap +Noun +A3sg +P2sg +Loc");
myWriter.Close();
p.WaitForExit();
var output = outputStringBuilder.ToString();
return output;
}
public static void Display(DataReceivedEventArgs nes)
{
Console.WriteLine(nes.Data);
}
I can read another text exe file output.
public static string Problem2()
{
ProcessStartInfo _startInfo = new ProcessStartInfo();
Process p = new Process();
StringBuilder outputStringBuilder = new StringBuilder();
string filePath = AppDomain.CurrentDomain.BaseDirectory + #"..\..\..\RTest\bin\debug\RTest.exe";
var file = new FileInfo(filePath);
p.StartInfo = _startInfo;
_startInfo.UseShellExecute = false;
_startInfo.RedirectStandardOutput = true;
_startInfo.RedirectStandardInput = true;
_startInfo.WorkingDirectory = file.Directory.FullName;
_startInfo.FileName = file.FullName;
p.OutputDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data);
p.Start();
p.BeginOutputReadLine();
var myWriter = p.StandardInput;
myWriter.AutoFlush = true;
myWriter.Close();
p.WaitForExit();
var output = outputStringBuilder.ToString();
return output;
}
Problem2 Method is success read output, I want read output Problem1 method.
I believe I am on the right track but just need a couple pointers.
Test project on the github
Something like this:
private string ReadProcessOutput(string fileName, TimeSpan waitTime, string args, string commandToEnter) // Command to enter in input window.
{
Console.WriteLine("Starting process: {0}", fileName);
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true
}
};
proc.Start();
proc.StandardInput.WriteLine(commandToEnter);
proc.WaitForExit((int)waitTime.TotalMilliseconds);
if (proc.HasExited)
{
Console.WriteLine("Process {0} exited with code {1}", fileName, proc.ExitCode);
string output = proc.StandardOutput.ReadToEnd();
Console.WriteLine("Process output: " + Environment.NewLine + output);
return output;
}
return null;
}

How to read process Output Message from process within the process?

I am running an exe through commandline and getting following output.
C:\Users\sysadmin>C:\Users\sysadmin\Desktop\New_folder\Setup\PatchInstaller.exe
--mode=silent
C:\Users\sysadmin Begin Setup UI mode: Silent Error :
Another instance running, Only a single instance can be run at a time.
Exit Code: 11
i am running this through System.daignostics.process.
My issue is PatchInstaller.exe calling another process and the output of that nested process is what is visible with cmd. but the same result and exit code i am not able to get through Process object of PatchInstaller.exe.
Is there any way of getting output of process running within process?
Following is the code i have tired...
string command = #"C:\Users\sysadmin\Desktop\Setup\PatchInstaller.exe";
string result = string.Empty;
System.Diagnostics.ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command + " --mode=silent);
System.Diagnostics.Process proc = new Process();
procStartInfo.ErrorDialog = false;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
if (!string.IsNullOrEmpty(domain) && !string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pwd))
{
procStartInfo.Domain = domain;
procStartInfo.UserName = user;
System.Security.SecureString ss = new System.Security.SecureString();
foreach (char c in pwd) { ss.AppendChar(c); }
procStartInfo.Password = ss;
}
proc = System.Diagnostics.Process.Start(procStartInfo);
proc.ErrorDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs errorLine)
{
if (errorLine.Data != null) result += "error:" + errorLine.Data +;
};
proc.OutputDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs outputLine)
{
if (outputLine.Data != null) result += outputLine.Data +;
};
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
Process[] pname = Process.GetProcessesByName("PatchInstaller");
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}
proc.WaitForExit();
I don't know much about ProcessStartInfo but I have used Process before and the way to get the information out of the standard output is shown as below, I assume it should be a similar way just by accessing the StandardOutput
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine(command);
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
var output = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
This code worked for me:
const int MAX_EXIT_WAIT_TIME = 3000;
// Fill needed data
string username = "";
string password = "";
string domain = "";
string appName = "";
var dir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
var appFullPath = Path.Combine(dir, appName);
ProcessStartInfo psi = new ProcessStartInfo(appFullPath);
psi.UserName = username;
var securePass = new System.Security.SecureString();
foreach (var c in password)
securePass.AppendChar(c);
psi.Password = securePass;
psi.Domain = domain;
psi.LoadUserProfile = false;
psi.WorkingDirectory = dir;
psi.Arguments = "";
psi.RedirectStandardOutput = true;
// Create Process object, but not start it!
var proc = new Process();
proc.StartInfo = psi;
StringCollection values = new StringCollection();
DataReceivedEventHandler outputDataReceived = (o, e) =>
{
lock (values)
values.Add(e.Data);
};
try
{
proc.OutputDataReceived += outputDataReceived;
// Only here we start process
if (!proc.Start())
throw new InvalidOperationException("Couldn't start app");
proc.BeginOutputReadLine();
proc.WaitForExit(MAX_EXIT_WAIT_TIME);
}
finally { proc.OutputDataReceived -= outputDataReceived; }
Console.WriteLine("Read {0} ", values.Count);
foreach (var item in values)
Console.WriteLine(" {0}", item);

Execute multiple command lines with output

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..

Getting data from Process.StandardOutput on the fly

I am trying to get data from Process.StandardOutput ... but I have a problem : i get the data when the process is ended, but not during execution (does it not flush ???) . It looks like the data is bufferred somewhere.
When I run the process manually the messages apper during execution. How to fix?
this is what I use to grab output from a process. This is adding to a stringbuilder, but you could do other things.
private void RunWithOutput(string exe, string parameters, out string result, out int exitCode)
{
ProcessStartInfo startInfo = new ProcessStartInfo(exe, parameters);
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
StringBuilder sb = new StringBuilder();
object locker = new object();
p.OutputDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs args)
{
lock(locker)
{
sb.Append(args.Data);
}
} );
p.ErrorDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs args)
{
lock (locker)
{
sb.Append(args.Data);
}
});
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit();
result = sb.ToString();
exitCode = p.ExitCode;
}

Categories