Disallow window creation in process - c#

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)

Related

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;

Redirecting Input of a process (notepad)

I am trying to open a Notepad process and write strings into it after initialization.
This is just a "POC" for my real goal which is to start a process, a user terminal of some sort, and completely control it through my app.
I searched the question here in different forms and found this link which was exactly what I was searching for!
Unfortunately, it doesn't work :/
This is the simple code:
static void Main(string[] args)
{
ProcessStartInfo ProcessInfo = new ProcessStartInfo("notepad");
ProcessInfo.RedirectStandardInput = true;
ProcessInfo.RedirectStandardOutput = true;
ProcessInfo.RedirectStandardError = true;
ProcessInfo.UseShellExecute = false;
ProcessInfo.ErrorDialog = false;
Process aProcess = new Process();
aProcess.StartInfo = ProcessInfo;
aProcess.Start();
StreamWriter processWriter = aProcess.StandardInput;
StreamReader processReader = aProcess.StandardOutput;
StreamReader processError = aProcess.StandardError;
while (!aProcess.Responding)
{
System.Threading.Thread.Sleep(5000);
}
processWriter.WriteLine("OMG IT FINALLY WORKED");
aProcess.WaitForExit();
}
The notepad process opens up but the information I tried to write with my processWriter is not present.
Does anyone have any idea why it's not working and how to make it work without walkarounds like using keystrokes and stuff?
Thanks in advance!

Process won't start minimized in 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

Running process from hidden window

I have faced a strange problem when trying to run a process from hidden window - the process I run runs in hidden like my process, Am I doing something wrong? I want to run that child process not hidden.
Process.Start(Path.GetTempPath() + "cleanup.exe", Path.GetDirectoryName(Application.StartupPath);
you can try with creating an object of Process class as below :
Process objProcess = new Process();
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// Passing the batch file/exe name
objProcess.StartInfo.FileName = string.Format(strBatchFileName);
// Passing the argument
objProcess.StartInfo.Arguments = string.Format(strArgument);
try
{
objProcess.Start();
}
catch
{
throw new Exception("Batch file is not found for processing");
}

C#: Run external console program as hidden

can anyone tell me how to spawn another console application from a Winforms app, but (A) not show the console window on the screen, and (B) still obtain the standard output of the application? Currently I have something like the following:
Process SomeProgram = new Process();
SomeProgram.StartInfo.FileName = #"c:\foo.exe";
SomeProgram.StartInfo.Arguments = "bar";
SomeProgram.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
SomeProgram.StartInfo.UseShellExecute = false;
SomeProgram.StartInfo.RedirectStandardOutput = true;
SomeProgram.Start();
SomeProgram.WaitForExit();
string SomeProgramOutput = SomeProgram.StandardOutput.ReadToEnd();
If I set RedirectStandardOutput to false, then the console app is hidden as expected, but I cannot get the standard output text. However, as soon as I set the RedirectStandardOutput to true, the window stops being hidden, although I am able to get the program's output.
So, I know how to make the console app run hidden, and I know how to get the program's output, but how do I get it to do both?
Many TIA
You are missing the CreateNoWindow property which has to be set to true in your case.
I think it will help you:
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = #"C:\Users\Vitor\ConsoleApplication1.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();

Categories

Resources