Running a command shell from C# - c#

I want to run a command from C# with below code but the application waits infinitely:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.Start();
p.WaitForExit();
p.StandardOutput.ReadToEnd();
I did a debug and the last break point passed is p.WaitForExit(). What am I doing wrong?
By the way, I tried run the command manually and I get the result in miliseconds.

Try prepending a /C to your arguments - without it, cmd.exe won't terminate

Related

C# Run console application from another app in a new console window

The second app is a console application and I want to see it's output window.
I know how to use Process.Start() but it doesn't show the console window for the app.
This is what I have tried:
Process.Start("MyApp.exe", "arg1 arg2");
So how to do it?
Perhapse this helps:
ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
info.CreateNoWindow = false;
info.UseShellExecute = true;
Process processChild = Process.Start(info);
I figured it out. I have to run cmd command with /k argument (to keep the console window open) and then my whole command-line:
var command = "MyApp.exe arg1 arg2";
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/k " + command);
processStartInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
//In case you need the output. But you have to wait enough for the output
//string text = process.StandardOutput.ReadToEnd();

How to stop the cmd window from closing

I run PsExec in a WPF application, but after execution, the window closes.
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\SysWoW64\PsExec64.exe";
process.StartInfo.Arguments = String.Format(#"\\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
I also tried:
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\SysWoW64\PsExec64.exe";
process.StartInfo.Arguments = String.Format(#" \K \\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
but nothing happens here. A window appears only for a second.
How can I stop the window from closing? Why doesn't "WaitForExit" do it?
Try running:
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
process.StartInfo.Arguments = String.Format(#"/k C:\Windows\SysWoW64\PsExec64.exe \\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
WaitForExit did not work for you because PsExec64.exe does not wait for user input at all. It take commands as an argument >> parse & run it >> exit process. So technically your "code" did wait for PsExec64.exe to exit and then continued.

C#, How to run a .bat file without a popup console window

I'm trying to run a .bat file without a popup console window.
I'm using this code:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "file.bat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
With this code, the program pops up a console window for a second and disappears. How to get it so that it never shows?
just add
p.StartInfo.CreateNoWindow=true;
Console window popup will not appear
p.StartInfo.CreateNoWindow = true;
Another option is
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
To use ProcessWindowStyle.Hidden, the ProcessStartInfo.UseShellExecute property must be false, but it looks like you were using that setting so you should be good.
The CreateNoWindow option must be set to true
p.StartInfo.CreateNoWindow = true;

Using New Process() in C#, how can I copy the command line text to a text file?

I want to run lmutil.exe with the arguments -a, -c, and 3400#takd, then put everything that command line prompt generates into a text file. What I have below isn't working.
If I step through the process, I get errors like "threw an exception of type System.InvalidOperationException"
Process p = new Process();
p.StartInfo.FileName = #"C:\FlexLM\lmutil.exe";
p.StartInfo.Arguments = "lmstat -a -c 3400#tkad>Report.txt";
p.Start();
p.WaitForExit();
All I want is for the command line output to be written to Report.txt
To get the Process output you can use the StandardOutput property documented here.
Then you can write it to a file:
Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = #"C:\FlexLM\lmutil.exe";
p.StartInfo.Arguments = "lmstat -a -c 3400#tkad";
p.Start();
System.IO.File.WriteAllText("Report.txt", p.StandardOutput.ReadToEnd());
p.WaitForExit();
p.Close();
You can't use > to redirect via Process, you have to use StandardOutput. Also note that for it to work StartInfo.RedirectStandardOutput has to be set to true.

How to pass command line argument in .net windows application

i want to pass a argument in c#.net to a console application i tried ProcessStartInfo
but that can be used for immediate run of an application ... but i want to set the arguments for the application which will run at scheduled time
Use the arguments propery to pass command line arguments
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments.aspx
Example:
var info = new System.Diagnostics.ProcessStartInfo();
info.FileName = "cmd.exe";
info.Arguments = "/C";
info.UseShellExecute = true;
var process = new System.Diagnostics.Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();

Categories