How to hide Application Window when its EXE has been called from a Process?

5.3k Views Asked by At

I'm running an application's EXE using a Class Library from a Service. But what i'm attempting to do is hide the application EXE's Window. Here is my code:

In my Class Library's function:-

public class MyClassLibrary
{
    public void MyFunction()
    {
        Process process = new Process();
        process.StartInfo.FileName = "C:\Program Files (x86)\MyFolder\MyApp.exe";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
    }
}

And this is where i'm calling it from:

class MyClass : ServiceBase
{
    ...
    ...
    ...
    protected override void OnStart()
    {
        MyClassLibrary obj = new MyClassLibrary();
        obj.MyFunction();
    }
}

Despite all of the above, the window is yet seen. Can anyone please suggest a solution?

Thanks and Regards, Siddhant

3

There are 3 best solutions below

0
HatSoft On

I have tried your code in does not work but

But when i try it this way it works fine

string filePath = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";    
ProcessStartInfo pStartInfo = new ProcessStartInfo(filePath );
pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Process.Start(startInfo);

Reason it works with Process.Start(ProcessStartInfo) because it associates the given information with a new component as explained on MSDN

0
Siddhant On

I've got the answer guys, thanks to this comment, which i got from Arne's comment at the top of this question.. Apparently it seems that the Process.StartInfo.UseShellExecute was supposed to be set to true.

Thanks everyone for helping out! Cheers!

0
gupta123 On
string filePath = @"C:\Windows\System32\notepad.exe";
ProcessStartInfo pStartInfo = new ProcessStartInfo(filePath);

**pStartInfo.UseShellExecute = true;** 

pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;   
Process.Start(pStartInfo);

Note: Set pStartInfo.UseShellExecute to true otherwise you get an error