Open Web Browser Window to URL C#


/ Published in: C#
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.