RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Stop Hijacking my Browser! 


qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbffc90000000000cb00000001000200

Often in applications you have the need to launch a browser window to open a specified URL. I recently evaluated various RSS readers where links to blog posts could be launched in an external browser window. What I found was that most of these applications would simply shell out the URL to launch and let Windows open it in the default application for http. The problem is that shelling the URL will pass it off to the default application, but not necessarily the a new instance of the default application. If there is an already running instance, then shelling the URL will simply cause the URL to be opened in that instance. Well, I hate that. I'll refer to that as hijacking my browser - even though that is a term that is more typically used for malicious spyware and nasty browser helper objects. So, repeat after me, “I will no longer hijack my users open brower windows from my applications“. But how do you go about that? Well, let's break it down and start by looking at the most common way to shell a URL from a .NET windows application and work our way up to making a generic way to launch a URL that will open in a new window every time.

Why not just change the setting in IE to not reuse windows? Telling the users of your application to make that change is just silly in my opinion. I wouldn't want to change that setting and have it effect every application just to get a single application to work the way it should. There are times when browser reuse makes sense - but there are also times when it does not. Besides, that applies to IE only, other browsers might not have that option.


Using the static Process.Start

When you typically see .NET code in a windows application that needs to launch a URL, you'll see the use of the static Start method of the Process class (found in the System.Diagnostics namespace). Something like this:

Process.Start("http://www.ryanfarley.com/");

There is nothing wrong with that. It will shell the URL out to the application configured as the default handler for http. However, shelling the URL like this does not cause a new instance to be started. If there are no currently running instances then of course one will be started, but in the case where you do have other browser windows running, the last opened window will be used to open the URL. That's no good, but this problem is not anything specific to Process.Start, you'd get the same results using the ShellExecute Win32API.


Shelling a new browser window

Something that you can do to force a new instance of the browser to be opened for the launched URL, is to shell an instance of the browser itself, instead of the URL, and pass the URL on the command-line to the browser. Any (good) browser I've ever used allows you to pass a URL on the command-line to it. When you shell the browser application and pass the URL on the command-line, a new browser window will be opened because it is the browser you are shelling. Here's an example that will launch a new instance of Internet Explorer and pass the URL to open on the command-line.

Process p = new Process();
p.StartInfo.FileName = "iexplore.exe";
p.StartInfo.Arguments = "http://www.ryanfarley.com/";
p.Start();

That will accomplish what we are after, but the big problem here is that now we have made it specific to Internet Explorer only. Your FireFox/Opera/whatever users (although their numbers may be small) won't be too happy about that. You'll often see code that uses COM interop to create an instance of InternetExplorer.Application or even worse, code to use SHDocVw as an embedded browser control (Ugh!). Neither of these are good since they both are specific to IE. What we need to do is something like the code above, but to first determine which application is the default application to handle http and use that instead of hard-coding “iexplore.exe”.


Determining the default browser

If we look in the registry we can easily determine which application is configured as the default browser. We'll need to find the application that will respond to a shell “open“ command for http. We can find that under HKEY_CLASSES_ROOT and look for the default value under the key HTTP\shell\open\command.

Make sure you check for the default application for http, not htm or html files or you might get an HTML editor instead of a browser.

When we look at that value, it might have other things in the string, not just the path & exe for the browser. For example, IE will have a value of something like "C:\Program Files\Internet Explorer\iexplore.exe" -nohome, FireFox will have a value like C:\PROGRA~1\MOZILL~1\FIREFOX.EXE -url "%1", etc. If we just trim it down to the path & exe only and pass the URL to that on the command-line then we'll be good in most cases. Something like this: (of course there are more efficient ways to get at just the full exe path from the string, but I'll just get rid of the quotes around it and trim off everything after the “.exe“ part, which is fine for this example)

private string getDefaultBrowser()
{
    string browser = string.Empty;
    RegistryKey key = null;
    try
    {
        key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

        //trim off quotes
        browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
        if (!browser.EndsWith("exe"))
        {
            //get rid of everything after the ".exe"
            browser = browser.Substring(0, browser.LastIndexOf(".exe")+4);
        }
    }
    finally
    {
        if (key != null) key.Close();
    }
    return browser;
}

That will return to us the full path to the default browser's executable. Now we can use that in the code we used earlier to launch the process (to shell the URL):

Process p = new Process();
p.StartInfo.FileName = getDefaultBrowser();
p.StartInfo.Arguments = "http://www.ryanfarley.com/";
p.Start();

That is it. That will work with the more common browsers (it will work with any of them that I've used). Now whether you think it is a good idea or not to shell all URLs in a new window, why make that decision for your users? Outlook 2003 launches all links in a new window, as do many other quality apps. At least give the user the option. In the RSS readers I evaluated I found that most of them would hijack my browser when launching external links. Another good thing about RSSBandit is that it also had the option to browse to a specific exe to launch when it would shell an external link. I could use that to browse to IEXPLORE.EXE to produce the same effect as described here - although it is always nice to see an option to let me choose if I want to always force the link to launch in a new window or not. Launching the URL on the command line to the browser will guarantee you happier users since they will no longer need to navigate back to pages that were open prior your launching a URL.




                   



Leave a comment below.

Comments

  1. RoudyBob 5/17/2004 6:51 AM
    Gravatar
    Great article, Ryan. You crack me up - "So, repeat after me, 'I will no longer hijack my users open brower windows from my applications'."
  2. Ryan Jameson 5/17/2004 9:14 AM
    Gravatar
    A most useful suggestion. I have two apps that I built that were doing this and it has bugged me since I released them. Both me and my users will be much happier now.
  3. C# Frequently Asked Questions 6/2/2004 11:48 AM
    Gravatar
  4. Tom Krueger 6/26/2004 1:20 AM
    Gravatar
  5. 봉시니 보물창고 6/26/2004 2:10 PM
    Gravatar
  6. André Teunissen 7/1/2004 2:55 AM
    Gravatar
    A was very pleased to see your solucion. It works fine also in Visual Baisc.Net

    But... I still do not know how to use this also for the shortcuts like ThisIsMe.url to open http://www.ThisIsMe.html

    Do you have an idea how to find the real document name (like the http full direction) "behind" a shortcut.

  7. Ryan Farley 7/1/2004 12:09 PM
    Gravatar
    André, to read the underlying URL in an internet shortcut (ie: a .url file) you just open it as text and read it as a INI file. For example, if I have a .url file that is a shortcut to my blog, when I open it as text it would look like this:

    [InternetShortcut]
    URL=http://www.ryanfarley.com/
    Modified=B00EC5CDA65FC4011A

    So I could wrap the INI API calls or just parse the text to get the URL and then continue with launching it.

    Good luck.
    -Ryan
  8. Ryan Farley 7/20/2004 7:09 AM
    Gravatar
    Arpanetguru,

    This does not work from an ASP.NET app. The call to Process.Start executes on the server, not the client. When launching an application on the client in an ASP.NET app you must use Javascript (or VBScript). Using Process.Start would execute in the context of the server, so you'd actually be attempting to launch a browser to www.google.com on the web server, not on the client's machine visiting the page (but you won't see a browser open on the server since it your code is running under the asp.net worker service account). What you are seeing is simply the postback to the server where it executes the code. Nothing in the code you posted happens in the context of the client.

    Try some searches for window.open using Javascript. If I get time later this week I'll post an example of how to do this from an ASP.NET C# application.

    -Ryan
  9. Just Coding 8/17/2004 2:49 AM
    Gravatar
  10. Wes 8/17/2004 8:33 AM
    Gravatar
    Thanks for the information. I currently working on an application (a replacement run command) that will benefit from this. However I think I'm going to make it an option to open in new window or not.
  11. .bering 8/18/2004 1:40 AM
    Gravatar
  12. iT_Ti 10/1/2004 3:32 AM
    Gravatar
    if Registry.ClassesRoot.OpenSubKey throws, then your finally statement will result in a NullReferenceException.

    TT
  13. Ryan Farley 10/9/2004 8:21 AM
    Gravatar
    iT_Ti,

    Good catch. Thanks. I'll update the code.

    -Ryan
  14. j@ck@ss 10/9/2004 4:30 PM
    Gravatar
    Ryan, you saved me a great deal of frustration, thank you. I knew the default behavior in I.E. was to “hijack” one of the open browser windows but I did not want my app to do that, especially if the only way to turn off that behavior is some obscure system setting that the average “joe user” would never find. I knew I was going to have to get the data out of the registry but I was thinking it would be much more complicated until I found your article, thanks again.

    j@ck@ss
  15. mechamonkey 11/15/2004 5:56 AM
    Gravatar
    Hi Ryan,
    Great article but I was wondering if you ever got chance to try out an ASP.Net / c# alternative to using window.open in Javascript ?

    Cheers.
  16. joe user 11/26/2004 2:18 PM
    Gravatar
    I am just a pc user who is trying to figure out how to fix this problem. All this info is great but I have no idea where to execute this information.Could you suggest a site where I might learn these basics. I just did a google search to identify the problem and possible solutions.
    clearly I am in over my head!
  17. Ryan Farley 11/30/2004 12:10 PM
    Gravatar
    Joe User,

    This post is really discussing something different, but I think what you're after is the setting to make IE not reuse windows, so it just works that way globally - for all apps that launch a browser window. Right?

    In IE, go to Tools, then Internet Options. Click on the Advanced Tab. Then under the Browsing section look for the setting called "Reuse windows for launching shortcuts". Uncheck this and each IE window launched will launch in it's own new window.

    -Ryan
  18. Sibil 3/27/2005 8:26 PM
    Gravatar
    How to do the same thing in pure C++? Any ideas?

    Cheers
  19. David 4/20/2005 5:35 AM
    Gravatar
    I would like to tack on Servlet parameters when I call Process.Start , but when IE comes up there is on a trailing character tacked on to the end of the URL which stops it from functioning correctly.

    My C# code looks like:

    Process.start ("http://mywebsite.com?name=David")

    but when IE instantiates the URL reads:

    Process.start ("http://mywebsite.com/?name=David")

    Is there any way to stop Process.start from adding the extra slash between '.com' and the '?' character

    Thanks!

    David

  20. Jake 4/27/2005 3:30 PM
    Gravatar
    Great stuff. Now, I don't see any documentation on setting the arguments so that I can specify the size of the window and also hide the toolbar and menu....any ideas?

    Thanks again!!!!
  21. Iassen Hristov 5/25/2005 11:55 AM
    Gravatar
    I would just like to note that Firefox does not suffer from this.
    It can be configured to always launch invoked URL's in a new tab or window.
  22. Ryan Farley 5/25/2005 12:01 PM
    Gravatar
    Iassen,

    That's correct. Just to point out (as mentioned in the post) that IE does not always suffer from this either. Both IE and FF can be configured to always open pages in a new window instead of reusing windows. However, the point of the post was to force the behavior if needed regardless of the setting in the browser.

    Thanks,
    -Ryan
  23. Justin 5/31/2005 9:58 AM
    Gravatar
    fantastic article, helped me greatly thanks a bunch
  24. JB 6/2/2005 8:32 AM
    Gravatar
    I know I'm nuts, but I need my C# application to use the existing browser window.

    I tried this:
    System.Diagnostics.Process.Start("http://www.google.com")

    And sure enough, it opened in the existing browser window. But my problem is that I want to open a local test.htm file in the existing browser window.

    I tried this:
    System.Diagnostics.Process.Start("c:\test.htm") and it went and opened the file in a NEW window!

    So I tried this:
    System.Diagnostics.Process.Start("file:///c:/test.htm") -- same problem, opened in a new window.

    Anyone know how I can get the file test.htm to open in the existing browser window?

    Thanks in advance!

  25. Jeff 6/2/2005 8:37 AM
    Gravatar
    I have used your code in a threaded application because Process.Start("http://www.ryanfarley.com/"">http://www.ryanfarley.com/") doesn't work in threaded mode.

    In my case, the Firefox doesn't open a new instance, the link just hijacks the current window.

    Even from the command line, Firefox won't open a new instance when I launch "firefox.exe http://www.ryanfarley.com/"">http://www.ryanfarley.com/"...

    any suggestion ?
  26. Ryan Farley 6/2/2005 8:55 AM
    Gravatar
    JB,

    To force the reuse of the existing browser window you'll need to use IE automation. This COM layer allows you to get an instance of a running IE window which you can then use to navigate it to a Uri or file.

    -Ryan
  27. Ryan Farley 6/2/2005 8:57 AM
    Gravatar
    Jeff,

    The code works ok for me with FireFox but I have not tried it the way you described. Since the code does use Process.Start, any issues with using Process.Start in a threaded mode will still apply here too.

    -Ryan
  28. Lavar Arrington 6/28/2005 9:30 AM
    Gravatar
    How about mac users. We're left in the dark too eh?
  29. Nix 7/1/2005 11:59 AM
    Gravatar
    This was an awesome article. The launch of my app will be a lot calmer than anticipated :)
  30. b0ne 7/3/2005 5:43 PM
    Gravatar
    Great article, thanks. I usually program in VB6, the old standby ShellExecute does the same thing there; hijacks any existing browser window (I'm assuming whichever one has the most recent hWnd?) instead of opening a new window, which irritates me to no end. I wish they'd have put in something like an "OpenNew" action for cases like this. In any case, would you happen to know how to force a new window in VB6?
  31. josh 7/11/2005 1:50 PM
    Gravatar
    Thanks for the article. It worked great on IE, but I'm experiencing some strange behavior in Firefox. If, when firefox is the default browser, I do

    p.StartInfo.Arguments = "www.ryanfarley.com";

    the process will force a new firefox window to open. If, however, I specify

    p.StartInfo.Arguments = "http://www.ryanfarley.com/";

    [note the http: presence], the app will hijack the current window (assuming firefox's Tools->Options->Advanced tells firefox to open in the most recent tab/window). Similarly, if I type "firefox.exe www.google.com" in the Run... box, it will open new, and if i type "firefox.exe http://www.google.com" it hijacks! Am I being a bonehead here?

    thanks,
    josh
  32. Ryan Farley 7/18/2005 2:00 PM
    Gravatar
    Josh,

    Great observation. You know, I tried it out and get the same results that you get. I'll play around with it and see what I come up with.

    -Ryan
  33. Ryan Farley 7/18/2005 2:36 PM
    Gravatar
    b0ne,

    In VB6 you can do essentially the same. ShellExecute allows for passing parameters. You simply provide the exe (iexplore.exe or firefox.exe, or dig it from the registry as I did above) and then pass the page as a command line argument in the call to ShellExecute.

    -Ryan
  34. Poorna 7/20/2005 6:38 AM
    Gravatar
    thanks alot
    it's great article
  35. Bill Massie 8/1/2005 8:02 AM
    Gravatar
    Josh,
    We're currently using COM interop to create an instance of InternetExplorer.Application to open a browser but we are switching over to something like what you describe here so that we can open the user's default browser.

    Opening the default browser works fine (and using the technique you describe here, we aren't even hijacking anymore), but using the COM interop method we were able to do things like hide the menubar and the address bar. e.g.:
    IE.MenuBar = False
    IE.AddressBar = False
    IE.ToolBar = False

    Is there any way we can do this w/o COM?

    Thanks for the great article,
    Bill
  36. Jean Dubois 8/2/2005 12:57 PM
    Gravatar
    Hi, I am fairly new to C# and I was wondering the following. I need to be able to start a few IE instances and browse through a series of URLs in each of them. Once I created and opened my IE windows, is it possible to send the process a new URL?

    What I want my code to do, basically, is
    open brower 1 at www.google.com
    open brower 2 at www.yahoo.com
    browser 1 goes to www.google.ca/search?hl=en&q=search&meta=
    browser 2 goes to search.yahoo.com/search?p=search
    etc...

    Thanks for your time, and for helping a C# newbie :)

    Jean
  37. kuldeep 9/22/2005 9:42 PM
    Gravatar
    That was a Gr8 Help Ryan.thanx. :)
  38. Juliette 10/14/2005 12:43 AM
    Gravatar
    God thing, if I can get it to work.

    I get this when I look for a reference in visual studio.
    RegistryKey(Microsoft.win32)
    No reference available for this item.

    I can't get it to work, since I need a reference to the win32. I have tried to find it on my computer, but there is non.

    Where can I find it.

    This should be easy, but that's the way it is.

    Thanks for help in advanse
  39. Ryan Farley 10/14/2005 7:39 AM
    Gravatar
    Juliette,

    All you need to do is add the Microsoft.Win32 namespace with a using statement (or Imports if you're using VB.NET) at the top of your code.

    -Ryan
  40. Gus 10/14/2005 2:58 PM
    Gravatar
    How about if I want to 'Hijack my Browser'? I have a VB 6.0 application that uses the old ShellExecute way to execute a URL but I want to use the same browse instance instead of popping up a new instance every time a browse session is called. Tried to use the window handle parameter but it is not working, it opened up a new IE session. Any ideas?

    Thanks,
    Gus
  41. Ryan Farley 10/14/2005 8:02 PM
    Gravatar
    Gus,

    Then *don't* use ShellExecute. The only way you can guarantee to reuse the same IE window is to use IE automation. You grab hold of the browser instance you want and then navigate it to the url you want. Of course, this means that it will *only* work with IE and no other browser.

    -Ryan
  42. Gus 10/17/2005 5:47 AM
    Gravatar
    Thanks, Ryan. Ideally I would like to be able to do this not only with IE. I though somehow this can be done by getting the window handle or some other 'magical' property of the browser application and keep using that instance of the application. The reason I want to do this (and not close and re-open the session) is that the current session of the browser has the userid/password and by re-using the same instance the user does not have to sign-on back to the session every time.
    Gus
  43. Eugene 11/5/2005 12:40 PM
    Gravatar
    Thanks for this, But... I'm trying to find a way to do the 'opposite'. My app opens in a new window but I don't know how to stop links from other apps opening in MY window.

    Thanks for any help.
  44. Wolf 1/17/2006 4:42 AM
    Gravatar
    Gus,

    You can use ShellExecute. All you need to do is to pass the URL as the executable, not the browser.

    -- Wolf --
  45. AA 1/18/2006 4:18 AM
    Gravatar
    Hi,
    I have a simple VC++ code which reads as
    HINSTANCE instance = ShellExecute(NULL,
    "open",
    "iexplore.exe",
    actualString.c_str(), //query string NULL,
    SW_MAXIMIZE );
    This opens up a new browser window, with Address bar. I need to hide the address bar and the menu bar.
    Any suggestions please...

    AA.
  46. Ryan Farley 1/18/2006 5:32 AM
    Gravatar
    AA,

    You'll have to use IE automation to do this. I don't know of any way to do this generically for any browser (but looks like that wouldn't be an issue since you're specifically launching IE already anyway)

    -Ryan
  47. Brian 2/9/2006 12:45 AM
    Gravatar
    Good article but I'm using VB6.0 and trying to prevent it from "hijacking" the browser. I'm using the shellexecute method - any suggestions on how to get it to use a new window? Basically want exactly what you managed to do with .NET but for 6.0.

    -Brian
  48. AVNEESH KUMAR 2/15/2006 2:41 PM
    Gravatar
  49. kris 4/5/2006 2:38 AM
    Gravatar
    I am not able to execute the GetDefaultBrowser() method..
    I am not able to use Registry...
    Do I need to use any namespace for that?
  50. Ryan Farley 4/5/2006 7:20 AM
    Gravatar
    kris,

    To use the code in the getDefaultBrowser function defined in this post, you must add the following namespaces:

    System.Diagnostics
    Microsoft.Win32

    -Ryan
  51. Kesney Fontes 4/14/2006 8:51 PM
    Gravatar
    Ryan, this solution rocks. Thank you!!!!
    btw, I added the following for opening the browser maximized:

    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;

    //Kesney
  52. dmaz 4/19/2006 4:12 PM
    Gravatar
    We have the same problem as Eugene above.

    "Thanks for this, But... I'm trying to find a way to do the 'opposite'. My app opens in a new window but I don't know how to stop links from other apps opening in MY window."

    Any Ideas?

    thanks
  53. Johan 7/19/2006 12:34 PM
    Gravatar
    Well, if you have Firefox and use the System.Diagnostics.Process.Start(url); Firefox will open the page in the same window, but, in a new tab. (could be altered in settings)
    That solution: same window, new tab is in my opinion the best. To bad IE doesnt have tabs yet :p
  54. Brigitte 8/4/2006 5:16 AM
    Gravatar
    Exactly what I was looking for BUT it doesn't compile with Delphi 2005 - I get "insecure code is only permitted in insecure procedures" in the line
    key := Registry.ClassesRoot.OpenSubKey(@'HTTP\shell\open\command', false);

    What on earth does "insecure code" mean? And how can I make it secure?

    TIA.
  55. Neelima Kapoor 1/28/2007 9:33 PM
    Gravatar
    Great article.. it works
  56. Wells 2/12/2007 1:08 AM
    Gravatar
    I could not launch the IE browser.
    I try to run it in windows 2003 server.

    Process p = new Process();
    p.StartInfo.FileName = "iexplore.exe";
    p.StartInfo.Arguments = "http://www.ryanfarley.com/";
    p.Start();

    It does not show the IE windows. However, in the task manager, there is a process of iexplore.exe.
    Any idea ?
  57. Holson Yap 2/22/2007 10:02 AM
    Gravatar
    Thank you very much

    This was really useful!

  58. Sam 3/8/2007 12:04 PM
    Gravatar
    In IE 7 URLs with query strings don't behave properly. The URL http://localhost/test/?test=1&dos=2">http://localhost/test/?test=1&dos=2 gets truncated to http://localhost/test/?test=1. Ouch.
  59. Alex 4/15/2007 11:53 AM
    Gravatar
    Nice code. Stuck it straight into my project.

    Much thanks!
  60. nikhil 5/3/2007 1:28 AM
    Gravatar
    Hi this is really awesome...
    Can some one tell me how to hide the address bar???
  61. AJ 8/30/2007 8:59 PM
    Gravatar
    Great article, however with IE7 a new process will start as a new tab in an existing browser process.

    Is there a way to avoid this and to have the process start as a completeley new browser?

    AJ
  62. Zac 9/13/2007 6:41 AM
    Gravatar
    Just a note that people might find helpful in Vista. The better key I've found to use for determining which browser the user likes would be:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice

    In Vista (at least my install of Vista), Firefox did not change the original registry key mentioned above. The only (albeit small) problem with the key above is it doesn't specifically name the location of the executable, but a smart programmer should be able to track that down fairly easily.... ;P

    AJ, if you're still watching this, if you want the process to start in a completely new browser, you have to change way I.E. behaves using Internet Options.
  63. Alvis Floyd 11/5/2007 9:02 AM
    Gravatar
    once i launch the new browser how can i vis the process.start
    how can i access the new borwser attributes?
    what i want to do is get the Handle of the new browser
    email alvis.floyd@ngc.com
  64. Joe N. 1/10/2008 5:57 AM
    Gravatar
    I have been using the System.Diagnostics.Process.Start for quite some time in vs2003 with out problem. Now I try and use the same exact function in vs2005 web services and I get two issues:

    1) it does not like the same syntax and makes me change it.

    Dim startexe As New Process
    startexe.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    startexe.Start(rt_getrootpath() + "importexe.exe")

    it wants me to change it to:
    process.Start(rt_getrootpath() + "importexe.exe")

    2) I get an error message that says "
    "No process is associated with this object"

    Any ideas
  65. Phil Newell 2/17/2008 2:45 PM
    Gravatar
    Awesome post, I have just spent 3 hours trying to use ShellExecute in Vista to open a URL from within a VB.net app. It just does not work. Using the Process approach all worked well.

    There is either a problem with ShellExecute, or I am juist stupid! This is the code that does NOT work under vista:
    Retval = ShellExecute(Me.Handle, vbNullString, "http://www.microsoft.com", vbNullString, vbNullString, vbNormalFocus)

    No error message, just no browser opening.

    I find many references to elevated permissions and prompts, as well as security problems and patches, but at the end of the day, the process function works correctly.

    Many Thanks
    Phil
  66. Mikkac 4/25/2008 5:20 AM
    Gravatar
    Hi Ryan,

    thanks for this code. Could you please tell me do you know where is stored default browser in windows Vista? I tried in "HTTP\shell\open\command" but IE is stored there even if Firefox is my default browser.

    Thanks in advance,
    Mikkac
  67. Andy 5/23/2008 8:28 AM
    Gravatar
    Hi,

    Does anybody know how to adapt this to an .exe file, im trying to create an autorun CD..

    Basically i want the above code to execute and load a html page into the default browser.

    I have packaged it up as an .exe file, but it goes through the insallation process but doesnt fire up the web browser.

    Can anybody give me some pointers please.

    Thanks

    Andy
  68. Ron 6/2/2008 9:50 AM
    Gravatar
    Ryan, I used your code below, but does not open a new window. I'm working with window forms trying to allow user to click button to open a web browser in a new window. Do you have some suggestions on how to make this work? Thanks!

    private void link_btn3_Click(object sender, EventArgs e)
    {

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "iexplore.exe";
    p.StartInfo.Arguments = "http://www.ryanfarley.com/";
    p.Start();

    }
  69. Gregory 6/6/2008 2:24 AM
    Gravatar
    Hi, I use nearly the exact code as you do and it works fine in development. One I hosted in the IIS, the process won't start as a user process. Instead, it becomes a NETWORK SERVICE. So I can't see what I want to see. My window is Vista Ultimate, IIS 7, and I'm using Visual Studio 2008. So anybody can me?
  70. Chris Morris 9/7/2008 9:18 PM
    Gravatar
    Is there code to open a hidden browser from the user?

    Chris
  71. 1/20/2009 12:19 PM
    Gravatar
    Abrir IExplorer con p?gina de inicio | hilpers
  72. Chandru 2/21/2009 12:46 AM
    Gravatar
    Hi Ryan,

    Thanks for the information.

    However, in Windows Vista, @"HTTP\shell\open\command" registry entry does not reflect the default browser. First we need to read "Progid" value under the subkey "HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice". In case of IE set as default, the value of Progid will be "IE.HTTP". Then, we need to browse to "HKEY_CLASSES_ROOT\<Progid>\Shell\open\Command" default value to get the default browser.

    Thanks,
    Chandru
  73. 3/26/2009 12:05 AM
    Gravatar
    Launch Default Browser with Users Home Page | keyongtech
  74. Grant 7/21/2009 8:12 AM
    Gravatar
    I've tried this code (with the Vista adjustments mentioned above) and I get some strange behaviour!

    Firstly, IE always opens it in a new window (good good). Firefox ALWAYS opens it in a new tab (bad bad).

    Google Chrome...well. If the currently running instance of Chrome HASN'T had focus, then running the code opens a new window. However, if one of these windows gets focus, the code will then open a new tab in that window! Crazy stuff!
  75. Ryan Farley 7/21/2009 9:05 AM
    Gravatar
    @Grant,

    All in all, it still is up to the browser how it will handle the OS handing it another URL to open. They all handle that differently. However, the point of all of this is to avoid navigating an open browser window away from some page the user is currently on - and that is still accomplished. When I wrote this (May 2004) IE was the predominant browser and Firefox was rising, to tabbed browsing was just barely out in Firefox and non-existent in IE. IE was especially bad at all this - I would have Outlook Web Access open in a browser and some app would launch a URL and navigate me away from OWA to the URL the app launched. Extremely annoying. Anyway, that is what started this all for me. Even though it is annoying that you can't really control whether it opens in a new window or a new tab, at least you can still accomplish the same of not taking over some already opened browser window. To be honest, I'm not even sure if this is still a problem. I don't believe it is anynore for FF and Chrome (IIRC default behavior is to open a new tab) and haven't checked if this has changed to IE since it has tabs now (the problem would still exist for sure for any IE6 users).

    Thanks for the comments.
    -Ryan
  76. Stephen 9/9/2009 3:35 PM
    Gravatar
    I am looking at implementing an online help file system for an application. The user selects some context sensitive help and it opens his browser to the correct page and anchor.

    I was using ShellExecute() for this, and the problem I am having is that every time you go to the help file, it opens a new instance in a new window.

    I have been trying to locate info on how to force it to stay in the same window. You talk about using IE automation, but I do not want to lock the behavior to forcing IE to be used - any idea how I can do this with whatever browser the user is using? Basically, ideally, I want to launch the first call to the help file in a new browser window, but then repeated calls to it would use the same window. Thanks!
  77. Mahmudul Alam 9/27/2009 1:08 AM
    Gravatar
    I need to detect whether flash and javascript is enabled or not in the default browser from a desktop c# code. Could anyone help me ?
  78. Daniel Bryars 10/5/2009 4:37 PM
    Gravatar
    Thanks for the article, I'm trying to offer the user a choice from the installed browsers on their machine.

    Do you know if there's a "good" way to enumerate the installed browsers in the system so I can shell out to the specific one the user chooses from a list?

    The reason I want to do this is that I have a "Studio" like application written in C# which lets my users edit their own web sites and publish them to my server - it would be awesome to let them view their pages in different browsers.

    Regards,

    Dan
  79. youtube 11/21/2009 10:02 AM
    Gravatar
    can i open firefox new tab
  80. Alan Yost 12/2/2009 6:09 PM
    Gravatar
    Works well on Vista SP2 / IE8.

    Thanks for the great article.

    Regards

    Alan
  81. Wen 7/27/2010 3:29 PM
    Gravatar
    Good Article. Help me hips.
  82. Wen 7/27/2010 3:31 PM
    Gravatar
    BTW, I think you might need to give it a better Title for better google ranking. Cos I search around for how to popup the default browser, I cannot believe your article is in second page.
  83. 8/29/2013 12:12 AM
    Gravatar
    <br /> Twitter OAuth using Twitterizer | Emad Mokhtar&amp;#039;s Framework
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:   @ryanfarley@mastodon.social

         

Sponsor

Sections