5

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: 
Community
  • 1
  • 1
Daniel Hamutel
  • 643
  • 1
  • 13
  • 30
  • `"http://stackoverflow.com/"` is not a valid path to an executable. If you want to open a URL, you will have to specify the browser executable there. But hiding the browser doesn’t really make much sense… – poke May 23 '15 at 15:00
  • See [this](http://stackoverflow.com/questions/12473343/how-to-minimize-ie-browser-using-c-sharp). – Yuval Itzchakov May 23 '15 at 15:20

2 Answers2

6

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);
Nathan
  • 1,303
  • 12
  • 26
  • Unlike the OP's original code, this doesn't open the URL with the default web browser. See [this (Windows-specific) SO question](http://stackoverflow.com/q/813058/240733 "Windows RegKey - Default Browser Application Path") to figure out the file path to the default web browser if you don't want to hard-code a specific web browser's executable filename. – stakx - no longer contributing Jun 28 '15 at 06:55
-1

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();
    }
Behzad
  • 3,502
  • 4
  • 36
  • 63