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;
Related
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();
I'm starting an exe via the Process class and I've noticed that the exe's output is being displayed inside my application's command window. *Note - when I start up the exe, I make sure a window is not opened - so, the only window that displays when my application is running is my main application, project.exe.
Is there a way to stop the exe's output from being displayed inside my project.exe command window? Here is my code:
Process process = new Process();
string exePath = System.IO.Path.Combine(workingDir, exeString);
process.StartInfo.FileName = exePath;
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (s, e) => Logger.LogInfo(e.Data);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
I've even tried setting RedirectStandardOutput to false with:
process.StartInfo.RedirectStandardOutput = false;
and the output is still placed in the command window.
This works when I tried locally on my box. Can you give it a try by replacing the exe path/name.
From MSDN doc's.
"When a Process writes text to its standard stream, that text is typically displayed on the console. By setting RedirectStandardOutput to true to redirect the StandardOutput stream, you can manipulate or suppress the output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file"
https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.110).aspx
void Main()
{
Process process = new Process();
string exePath = System.IO.Path.Combine(#"C:\SourceCode\CS\DsSmokeTest\bin\Debug", "DsSmokeTest.exe");
process.StartInfo.FileName = exePath;
process.StartInfo.WorkingDirectory = #"C:\SourceCode\CS\DsSmokeTest\bin\Debug";
process.StartInfo.Arguments = string.Empty;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (s, e) => Test(e.Data);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}
// Define other methods and classes here
public void Test(string input)
{
input.Dump();
}
just add the following line
process.StartInfo.UseShellExecute = true;
I have been messing around with triggering a bash script via C#. This all works fine when I first call the "open" command with arguments which in turn opens my .command script via Terminal.
Once the "open" command is used once Terminal or iTerm will remain open in the background, at which point calling the "open" command with arguments then has no further effect. I sadly have to manually quit the application to trigger my script again.
How can I pass arguments to an already open terminal application to restart my script without quitting?
I've searched online ad can't seem to work it out, it already took a good amount of time solve the opening code. Your help is much appreciated.
Here is the C# code I'm using to start the process:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "open";
p.StartInfo.WorkingDirectory = installFolder;
p.StartInfo.Arguments = "/bin/bash --args \"open \"SomePath/Commands/myscript.command\"\"";
p.Start();
Thanks
EDIT:
Both answers were correct, this might help others:
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
startInfo.WorkingDirectory = installFolder;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("echo helloworld");
process.StandardInput.WriteLine("exit"); // if no exit then WaitForExit will lockup your program
process.StandardInput.Flush();
string line = process.StandardOutput.ReadLine();
while (line != null)
{
Debug.Log("line:" + line);
line = process.StandardOutput.ReadLine();
}
process.WaitForExit();
//process.Kill(); // already killed my console told me with an error
You can try:
before calling p.Start():
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
// for the process to take commands from you, not from the keyboard
and after:
if (p != null)
{
p.StandardInput.WriteLine("echo helloworld");
p.StandardInput.WriteLine("executable.exe arg1 arg2");
}
(taken from here)
This is what you may be looking for :
Gets a stream used to write the input of the application.
MSDN | Process.StandardInput Property
// This could do the trick
process.StandardInput.WriteLine("..");
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
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.