Daniel Hamutel
Daniel Hamutel

Reputation: 653

How can i start process and hide it's window?

I tried this:

ProcessStartInfo psi = new ProcessStartInfo("https://stackoverflow.com/");
            psi.RedirectStandardOutput = false;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.UseShellExecute = false;

            Process.Start(psi);

But i'm getting exception on the line Process.Start(psi);

Win32Exception The system cannot find the file specified

If i change the line psi.UseShellExecute = true; Then it's working but it dosen't hide the window.

I want that when it's opening the broswer for example to https://stackoverflow.com/ the user will not see the window at any time but that the window will still be opened. It's not to close but hiding it.

Tried to google but didn't find a working solution.

The Win32Exception message:

System.ComponentModel.Win32Exception was unhandled
  HResult=-2147467259
  Message=The system cannot find the file specified
  Source=System
  ErrorCode=-2147467259
  NativeErrorCode=2
  StackTrace:
       at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
       at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
       at CuteDadyImages.Form1.OpenBroswerTab() in d:\C-Sharp\test\Form1.cs:line 155
       at CuteDadyImages.Form1..ctor() in d:\C-Sharp\test\Form1.cs:line 55
       at CuteDadyImages.Program.Main() in d:\C-Sharp\test\Program.cs:line 35
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Upvotes: 5

Views: 7375

Answers (2)

Behzad
Behzad

Reputation: 3590

You can find all open forms from ApplicationOpenForms and then you can hidden all of them, but one problem exist, that array is list of just open forms and when one of them closed or hide then that form removed from list and your For or Foreach loop go to exception!

Test this codes to hide all open forms after after open your URL:

    Process.Start("http://stackoverflow.com/");

    List<Form> oForms = new List<Form>();
    foreach (Form form in Application.OpenForms)
    {
        oForms.Add(form);
    }

    foreach (var form in oForms)
    {
        form.Hide();
    }

Upvotes: -1

Nathan
Nathan

Reputation: 1317

Add the following somewhere in your code

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

And then try starting the browser using the following:

        var process = new Process
        {
            StartInfo =
            {
                FileName = "firefox.exe",
                Arguments = "http://stackoverflow.com/",
                CreateNoWindow = true,
                ErrorDialog = false,
                WindowStyle = ProcessWindowStyle.Hidden
            }
        };
        process.Start();
        Thread.Sleep(1000);
        ShowWindow(process.MainWindowHandle, 0);

Upvotes: 6

Related Questions