Search code examples
winformsbrowserlaunching-application

Launch default web browser, but not if URL already open


I have a link on my app UI that launches a URL using System.Diagnostics.Process.Start(). If the user clicks the link several times, it opens several tabs.

Is there a way, maybe a command-line option, to still use the default web browser, but have it just reopen the same tab if the URL is already open? It would be OK if it doesn't work with every possible browser out there, but nice if it at least works with IE, Firefox and Chrome.

I doubt it, but since I didn't see any other questions/answers on this topic, I figured I'd ask.


Solution

  • This is somewhat of a workaround but it might get you started. I have used the System.Diagnostics.Process.ProcessId. As an example I have used IE, I will explain later why I did this. The code is just "quick and dirty" but I just made it as proof of concept.

    I have created a basic WinForm app with one button that will open google in IE, if it has already been opened by the application it will not be opened again.

    I added the System.Diagnostics reference.

        public int ProcessID;
        public Form1()
        {
            InitializeComponent();
        }
        private void MyButton_Click(object sender, EventArgs e)
        {
            if (ProcessID == null)
            {
                StartIE();
            }
            else
            {
                if (!ProcessIsRunning())
                {
                    StartIE();
                }
            }
        }
        private bool ProcessIsRunning()
        {
            bool ProcessRunning = false;
            foreach (Process p in Process.GetProcesses())
            {
                try
                {
                    if (p.Id == ProcessID)
                    {
                        ProcessRunning = true;
                    }
                }
                catch { }
            }
            return ProcessRunning;
        }
        private void StartIE()
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "iexplore.exe";
            proc.StartInfo.Arguments = "http://www.google.be";
            proc.Start();
            ProcessID = proc.Id;
        }
    

    This does not completely do what you requested but it might be a good start. There are a few reasons why I did it this way and what possible options are..

    • If you would use the url as the Filename, it would indeed open up the webpage in the default browser, it would however not return a processID. This is why the snippet shows usage of IE. (If you would use this option, you could use the System.IO.File.Exists to make sure the desired browser is installed)
    • If you would like to use this option, you can query the registry to pick up what te default browser is, if you have that you could launch that from the value obtained from the registry. If you then change the process.startinfo.filename to this value, then you will launch the default browser but you will still obtain a processId so this might be the way forward. You can check how to do this over here: http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/b200903e-ce69-4bd4-a436-3e20a7632dc4
    • Showing the internet window if it would already be opened, can be done by using the SetForegroundWindow property. As this is already documented in this article, I did not add it in this snippet.

    I hope this helps to get you on your way.