Add link opening to Browser class

This commit is contained in:
2014-05-16 14:14:28 -04:00
parent 83f6aaaf61
commit 794f86d84d
2 changed files with 57 additions and 14 deletions

View File

@@ -1,38 +1,81 @@
using System.Collections.Generic; using Microsoft.Win32;
using Microsoft.Win32; using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Common.Internet namespace Common.Internet
{ {
public class Browser public class Browser
{ {
public string Key { get; private set; }
public string Name { get; private set; } public string Name { get; private set; }
public string Command { get; private set; } public string Command { get; private set; }
public string DefaultIcon { get; private set; } public string DefaultIcon { get; private set; }
public bool SupportsPrivate()
{
return false;
}
public override string ToString() public override string ToString()
{ {
return Name; return Name;
} }
public bool OpenLink(string url, bool privateMode)
{
// Don't bother with empty links
if (String.IsNullOrEmpty(url))
return true;
// Add quotes around the URL for safety
url = string.Format("\"{0}\"", url);
if (privateMode)
url += " -incognito";
// Start the browser
Process.Start(Command, url);
return true;
}
public static Dictionary<string, Browser> DetectInstalledBrowsers() public static Dictionary<string, Browser> DetectInstalledBrowsers()
{ {
var browsers = new Dictionary<string, Browser>(); var browsers = new Dictionary<string, Browser>();
RegistryKey browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet"); var browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
if (browserKeys == null) if (browserKeys == null)
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet"); browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
string[] browserNames = browserKeys.GetSubKeyNames(); if (browserKeys == null)
return browsers;
foreach (string browserName in browserNames) var browserNames = browserKeys.GetSubKeyNames();
foreach (var browserName in browserNames)
{ {
Browser browser = new Browser(); var browserKey = browserKeys.OpenSubKey(browserName);
RegistryKey browserKey = browserKeys.OpenSubKey(browserName);
browser.Name = (string) browserKey.GetValue(null); if (browserKey == null)
RegistryKey browserKeyPath = browserKey.OpenSubKey(@"shell\open\command"); continue;
browser.Command = (string) browserKeyPath.GetValue(null);
RegistryKey browserIconPath = browserKey.OpenSubKey(@"DefaultIcon"); var browserKeyPath = browserKey.OpenSubKey(@"shell\open\command");
browser.DefaultIcon = (string) browserIconPath.GetValue(null);
if (browserKeyPath == null)
continue;
var browserIconPath = browserKey.OpenSubKey(@"DefaultIcon");
var browser = new Browser
{
Key = browserName,
Name = (string) browserKey.GetValue(null),
Command = (string) browserKeyPath.GetValue(null),
DefaultIcon = browserIconPath == null ? null : (string) browserIconPath.GetValue(null)
};
browsers.Add(browserName, browser); browsers.Add(browserName, browser);
} }

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Microsoft.Bcl" version="1.1.8" targetFramework="net40" /> <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" /> <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
</packages> </packages>