Trouble running Git from C# - c#

I'm trying to run some Git commands from a C# program using System.Diagnostics.Process and it's not working. Git is in my path and when I try to run the command at the command prompt it works fine. I've tried capturing the standard output using a nested using() statement and that's not helping. When it gets to the reader.ReadToEnd() it just shows the DOS window hung with nothing in it. Here's my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
startInfo.WorkingDirectory = "c:\\gitmover";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = " git add \"*.*\"";
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process3.StandardOutput)
{
string result = reader.ReadToEnd();
}
}
Any ideas what I could be doing wrong?

Related

How to run an exe file using C# in console application

I am trying to run an exe file in my console application which is located on a network drive.
So what needs to happen is the app needs to map the network drive with a drive letter by using this code:
private static void MapDrive()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "net.exe";
startInfo.Arguments = #"use w: \\<server>\CompanyData\W10 /user:Administrator Password";
process.StartInfo = startInfo;
process.Start();
}
This works great and the drive letter is mapped.
Now the problem I am facing is to run the exe file with in this mapped drive.
I have tried the below but does not seem to work:
private static void RunSetup()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"w:\setup.exe";
process.StartInfo = startInfo;
process.Start();;
}
Nothing seems to happen in regards to launching the exe file.
I need to know what I am doing wrong here?
Thanks
Either include the "/c" argument
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/c w:\setup.exe";
or set FileName directly to the setup.exe
startInfo.FileName = "w:\setup.exe";
as mentioned in the comments

cmd command don't execute in C#

I want to run cmd command with C# for install service in Windows, I'm using of below code:
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments =
"\"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe\" \"D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe\"";
process.StartInfo = startInfo;
process.Start();
}
}
but this program don't work. if I run this command in cmd.exe work properly but when I run this project don't execute command and MyNewService.exe don't install.
where is my problem?
do you help me?
Instead of starting a cmd.exe and passing installutil as an argument (then your service executabel an argument of the argument), try starting the installutil.exe executable directly passing the MyNewService.exe as the argument.
You should always wait for the process to exit, and should always check the exit code of the process as below.
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe";
startInfo.Arguments = "D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe";
process.StartInfo = startInfo;
bool processStarted = process.Start();
process.WaitForExit();
int resultCode = process.ExitCode;
if (resultCode != 0)
{
Console.WriteLine("The process intallutil.exe exited with code {0}", resultCode);
}
}

Silent Installation of exe and Launch the Application

I am going to install the my project silently using the following options from the C#.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = true;
startInfo.FileName = tempPath + "myproject.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "/s /v/qn";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
exitcode = exeProcess.ExitCode;
}
It installs my projects sucessfully.but I need to Launch the installed application after the installation. How I can Do this? Is there any Additional arguments I need to pass to Launch the product after the installation?
Thanks in Advance,
Roshil

Starting a batch file in C# and then waiting for it to exit before continuing

The problem is that the WaitForExit does not wait until the batch file quits. It comes back almost right away.
I'm starting my batch file as follows:
ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
startInfo.UseShellExecute = true;
startInfo.Arguments = arguments;
using (Process p = Process.Start(startInfo))
{
p.WaitForExit();
}
I tried with and without UseShellExecute.
You could try running cmd with a "/c yourbatchfile" as command line arguments instead.
It seems that you can do it redirecting the StdOut and read it until it closes.
Took this idea from this similar question.
Adapting your snippet, that would be:
ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
//startInfo.UseShellExecute = true;
startInfo.Arguments = arguments;
startInfo.RedirectStandardOutput = true;
Process p = Process.Start(startInfo);
String output = proc.StandardOutput.ReadToEnd();

How to run commands on Mingw from other process with C#?

I am trying to execute commands on Mingw from other process with this code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"PATH-TO-MINGW\mingwenv.cmd";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
using (Process exeProcess = Process.Start(startInfo))
{
StreamWriter str = exeProcess.StandardInput;
str.WriteLine("ls");
exeProcess.WaitForExit();
}
but this code just lunches Mingw and does not input command.
Do I miss something or it is not possible to do?
Thanks
Update
Based on Jason Huntleys answer, solution for me looks like this (I am using OMNeT++ simulator so directories are based on it)
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"PATH_TO_SIMULATOR\omnetpp-4.3\msys\bin\sh.exe";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
using (Process exeProcess = Process.Start(startInfo))
{
using (StreamWriter str = exeProcess.StandardInput)
{
str.WriteLine("cd PATH_TO_SIMULATOR/omnetpp-4.3");
str.Flush();
str.WriteLine("ls");
str.Flush();
}
exeProcess.WaitForExit();
}
I suspect c# is launching your mingw command in a CMD prompt. You need to spawn your process within a bash shell. Try wrapping your command with "bash -l -c 'ls'" or "bash -c 'ls'". Make sure bash is in your PATH, and be sure you quote command arguments, if any. I've had to use this method when I spawn bash commands from popen in python. I know diff language, but could be related.
I imagine the code will look similar to this. I haven't tested in C#:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "bash.exe";
startInfo.Arguments = "-l -c 'ls -l /your/msys/path'";
# Or other examples with windows path:
# startInfo.Arguments = "-l -c 'ls -l /c/your/path'";
# startInfo.Arguments = "-l -c 'ls -l C:/your/path'";
# startInfo.Arguments = "-l -c 'ls -l C:\\your\\path'";
process.StartInfo = startInfo;
process.Start();
you should do
str.Flush();
so the command you've writen is passed to the process.
also you should use using statement when dealing with streams
using (Process exeProcess = Process.Start(startInfo))
{
using(StreamWriter str = exeProcess.StandardInput)
{
str.WriteLine("ls");
str.Flush();
exeProcess.WaitForExit();
}
}

Categories