I posted something somewhere on here a while ago and a few people contacted me asking me for help dealing with browsers. I forgot I had this and found it earlier, so figured I could post it up here for anyone that'd find it useful.
Ignore my logging stuff. Any questions feel free to post.
Code
class browserHandler
{
genericHandler gHandler = new genericHandler();
public string getBrowser()
{
const string userChoice = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";
string progId;
using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(userChoice))
{
if (userChoiceKey == null)
{
return "unsupported";
}
object progIdValue = userChoiceKey.GetValue("Progid");
if (progIdValue == null)
{
return "unsupported";
}
progId = progIdValue.ToString();
switch (progId)
{
case "IE.HTTP":
return "iexplore";
case "FirefoxURL":
return "firefox";
case "ChromeHTML":
return "chrome";
default:
return "unsupported";
}
}
}
public string getFFURL()
{
DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
dde.Disconnect();
return url;
}
public string getIEURL()
{
genericHandler gHandler = new genericHandler();
string fileName;
foreach (InternetExplorer ie in new ShellWindows())
{
fileName = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if ((gHandler.isInteresting(ie.LocationURL.ToString()) && (fileName.Equals("iexplore"))))
{ return ie.LocationURL.ToString(); }
else { continue; }
}
return null;
}
public string getChromeURL()
{
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome)
{
// the chrome process must have a window
if (chrome.MainWindowHandle == IntPtr.Zero)
{
continue;
}
// find the automation element
AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
// if it can be found, get the value from the URL bar
if (elmUrlBar != null)
{
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
if (!val.Current.Value.Contains("https"))
{
string link = "http://" + val.Current.Value;
// gHandler.logMsg("Found [" + link + "]");
return link;
}
else
{
gHandler.logMsg("Found [" + val.Current.Value + "]");
return val.Current.Value;
}
}
else { return "error"; }
}
}
return "error";
}
}Ignore my logging stuff. Any questions feel free to post.

