Process won't start minimized in c# - c#

pro.StartInfo.FileName = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
pro.StartInfo.Arguments = a;
pro.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
pro.Start();
I have this code above which starts Firefox minimized. but Firefox does not actually start minimized but as a normal window. What is the problem with my code? Do I have to sleep the thread for 100 ms ?

Try out this one :)
pro.StartInfo.FileName = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
pro.StartInfo.Arguments = a;
pro.UseShellExecute = true;
pro.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
pro.Start();
As I think this will only work if firefox is NOT running. Else it will still open firefox, but not minimized. If you want to minimize your own starting firefox if the process is already up, you will need to handle ShowWindow as described here.

to start a process without opening a terminal windows one can do like this:
ProcessStartInfo pro = new ProcessStartInfo();
pro.FileName = 'pathToFile'
pro.RedirectStandardInput = true;
pro.RedirectStandardOutput = false;
pro.Arguments = 'some arguments'
pro.UseShellExecute = false;
pro.CreateNoWindow = true; // <- imp. line

Related

c# vsto calling exe , works only half of the times

Hello everyone and thank you for the help
I made a word Addin while clicking some of the buttons like upload file or login,c# calling exe to run
But for some reason, the exe does not work each time I try, only half of the time, and if I succeed, when I am trying to call the exe again for a different purpose, I failed (nothing happend)
one more thing, I need to get the output of the exe file.
the exe in the debug folder
the code below:
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = Path.GetDirectoryName("Api_Layer.exe");
processInfo.FileName = "Api_Layer.exe";
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
Process proc = Process.Start(processInfo);
List<string> output = new List<string>();
while (!proc.HasExited)
{
output.Add(proc.StandardOutput.ReadToEnd());
}

Why does Kaspersky anti-virus prevent my program from capturing the output of its child process?

I have a C# program that launches a child process and captures its output in a string. This works on most Windows machines (Windows 7 and newer), but when Kaspersky anti-virus is present, Process.StandardOutput.ReadToEnd() returns null. There is no error code or exception. The child process is a trusted console application. The process takes 5 or 6 seconds to run.
The code for launching the child process is as follows:
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = "icao.exe";
psi.Arguments = im_path + "image.jpg";
Process p = new Process();
p.StartInfo = psi;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
MessageBox.Show(error);
p.WaitForExit();
int exitCode = p.ExitCode;
MessageBox.Show(exitCode+"");
Why does output end up being null when Kaspersky is present?
My guess is that Kaspersky's heuristics are seeing that your program wants to execute another exe. Because nothing is telling Kaspersky that this is ok, it flags your program as possible malware, because it wants to interface with other programs that are developed by other companies. If you are able to I would try white listing your program with Kaspersky and see if that solves your issue.

How to have single command window for multiple processes in c#

I have a winforms application where I run a process (code shown below) in a loop. Suppose I have 10 items, I get 10 command windows where the actions are run. So, I wanted to know if there is a way where I can run the process 10 times but have just one command window open where all my actions are run.
Process p = new Process();
try
{
var pInfo = new ProcessStartInfo(executable, args);
pInfo.UseShellExecute = false;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
pInfo.WorkingDirectory = workingDir;
p.StartInfo = pInfo;
p.EnableRaisingEvents = true;
SubscribeEvents(p);
p.Start();
p.WaitForExit();
}
I guess if you are doing winforms, you actually already probably have a main window open.
You can use this to hide your other windows:
pInfo.CreateNoWindow = true;

Disallow window creation in process

I am creating a process to do some working. But the process is starting a black window (like the cmd). I have tried to set the CreateNoWindow = true but that doesn't help.
Is there another way to disallow window creation?
Here is my code:
var worker1 = new Process();
worker1.EnableRaisingEvents = true;
worker1.StartInfo.CreateNoWindow = true;
worker1.StartInfo.ErrorDialog = true;
worker1.StartInfo.Arguments = job.BtcFilePath;
worker1.StartInfo.FileName = job.ExeFilePath;
worker1.Exited += new EventHandler(Worker1Exited);
Processors.Add(worker1);
Processors.Last().Start();
Processors.Last().PriorityClass = ProcessPriorityClass.BelowNormal;
*The Processors is a list of processors
BR
worker1.StartInfo.CreateNoWindow = true;
worker1.StartInfo.UseShellExecute = false;
This worked for me in a similar situation.
worker1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
This seems to be another option. (Did not test this, but found this while browsing http://www.dotnetperls.com/png)

CMD command not running in console

I have some code that runs a cmd command in C# which works really well in a WinForm but when running this in a console app it doesn't work. I am a bit stuck as to why this is, I tried adding Windows.Forms as a reference and added the using to the code but this didn't work either. The only other thing I can think of is that because it is running as a console it can't run another console window on top of this?
Any help is appreciated.
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process console = Process.Start(cmd);
console.StandardInput.WriteLine("command to run");
The following code will perform any console command you want and output the console text in your current window, everything after while(true) is just as example:
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = false;
cmd.WindowStyle = ProcessWindowStyle.Normal;
Process console = Process.Start(cmd);
while(true)
console.StandardInput.WriteLine("pause");
If you don't want any console output then set CreateNoWindow to true. Also this code works inside a console application using System.Diagnostics
Good luck!

Categories

Resources