Posted By

Verician on 09/26/12


Tagged

url browser open web


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

alex001
thastyle
Priestd09
danfinrud55


Open Web Browser Window to URL C#


 / Published in: C#
 

I needed to open a browser window from my small C# app as a result of a dynamic URL build from data received from the command line. This did the trick!

  1. using System.Diagnostics;
  2. using Microsoft.Win32;
  3.  
  4.  
  5. private static string GetDefaultBrowserPath()
  6. {
  7. string key = @"htmlfile\shell\open\command";
  8. RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(key, false);
  9. return ((string)registryKey.GetValue(null, null)).Split('"')[1];
  10. }
  11.  
  12.  
  13. static public void openBrowser(string theURL)
  14. {
  15. string defaultBrowserPath = GetDefaultBrowserPath();
  16. try
  17. {
  18. Process.Start(defaultBrowserPath, theURL);
  19. }
  20. catch (Exception exp)
  21. {
  22. Console.WriteLine(exp.Message);
  23. System.Diagnostics.EventLog.WriteEntry("ERROR", exp.Message, System.Diagnostics.EventLogEntryType.Error);
  24. }
  25. }

Report this snippet  

You need to login to post a comment.