DefaultBrowser from Registry doesn't work - c#

I'm trying to open a url in the default browser. Obviously I thought that Shell Exec will open it in the default browser but it doesn't.
Then I tried explicit:
Process.Start(GetDefaultBrowserPath(), "http://stackoverflow.com");
private static string GetDefaultBrowserPath()
{
string key = #"htmlfile\shell\open\command";
RegistryKey registryKey =
Registry.ClassesRoot.OpenSubKey(key, false);
// get default browser path
return ((string)registryKey.GetValue(null, null)).Split('"')[1];
}
It always returns Internet Explorer but not my default which is Firefox. I tried it on several computers...
I don't care which way to call the link in the default browser, but it has to be the default

Have you tried just running:
Process.Start("http://stackoverflow.com");
My test application (below) opens the site in my default browser:
using System;
using System.Diagnostics;
namespace ProcessStartSample
{
class Program
{
static void Main(string[] args)
{
Process.Start("http://stackoverflow.com");
}
}
}
In other words, let the operating system do the heavy work of working out what the users default browser is for you! =)

Just Try this :)
Process.Start("http://stackoverflow.com");
And if you want to find your default browser you should open HKEY_CLASSES_ROOT\http\shell\open\command\default key.
Please pay attention "http" not "htmlFile"
EDIT:
CODE:
RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(#"http\shell\open\command", false);
string value = registryKey.GetValue("").ToString();

Windows will start the default browser on the user's system for you automatically if you just specify the URL to open:
Process.Start("http://www.google.com/");
No need for any fancy Registry trickery, unless you are trying to ascertain which browser is set as the default.

Related

How to set the WebBrowser object in the .NET Framework to use whatever highest version of IE is installed on the users system

So the title says it all, I would like C# code (so please, PLEASE make sure it isn't Visual Basic code). And that is all I want to ask. I have tried the web browser built in to the .NET framework, but it looks like some old version of IE (if I am right or not). And if you answered, well thanks I guess! I need this for a small project where a bot would just log on to a website (its a base for future projects).
By default it's IE7. You can bang a registry entry in to make it later:
public static void EnsureBrowserEmulationEnabled(string exename = "YourAppName.exe", bool uninstall = false)
{
try
{
using (
var rk = Registry.CurrentUser.OpenSubKey(
#"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true)
)
{
if (!uninstall)
{
dynamic value = rk.GetValue(exename);
if (value == null)
rk.SetValue(exename, (uint)11001, RegistryValueKind.DWord);
}
else
rk.DeleteValue(exename);
}
}
catch
{
}
}
Code courtesy of this blog
The values you can use in place of 11001 can be found in MSDN
Alternatively; can you do what you want by using WebClient/HttpWebRequest rather than poking at a web browser control to navigate around? Or can you find some web service/api version of the site that will respond with JSON rather than trying to manipulate html?
I was mildly curious why you'd care what a page looks like if it's a bot that is using it, but perhaps you're hitting a "your IE is too old" from the server..

C#: Cannot see the changes in the windows registry

No error, no exception, no nothing. Everything seems to be OK, except that the registry remain as it is.
class Program
{
static void Main(string[] args)
{
try
{
Edit();
}
catch (Exception)
{
Restore(); // not included in the sample for simplicity
}
}
public static void Edit()
{
Microsoft.Win32.RegistryKey Login;
Login = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(ConfigurationManager.AppSettings["Login"].ToString());
Login.SetValue("ServerName", ConfigurationManager.AppSettings["ServerName"].ToString());
Login.SetValue("ImageServerName", ConfigurationManager.AppSettings["ImageServerName"].ToString());
Login.Close();
Microsoft.Win32.RegistryKey Login2;
Login2 = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(ConfigurationManager.AppSettings["Wow6432NodeLogin"].ToString());
Login2.SetValue("ServerName", ConfigurationManager.AppSettings["Wow6432NodeServerName"].ToString());
Login2.SetValue("ImageServerName", ConfigurationManager.AppSettings["Wow6432NodeImageServerName"].ToString());
Login2.Close();
}
}
I think there's is an error somewhere. But no exception is thrown. The catch block never gets hit.
I'm running it as Admin. I even ran it with no admin privileges, but still no errors when it supposed to show "access denied" or something. I restarted the laptop to see the changes applied, but still no success.
I used this code to read the recently added values and I can see the keys. But somehow the changes are not being applied.
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(ConfigurationManager.AppSettings["Login"].ToString());
Object o = key.GetValue("ServerName");
Console.WriteLine(o.ToString());
I'm using .Net 4.5.2, building for Any CPU, SO: Windows 7.
Do I need to commit the changes or something?
As suggested by #PieterWitvoet in the comments, you might want to use OpenBaseKey() instead. This is to avoid WoW64 registry redirection as explained here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa384182.aspx
Note the small print at the end of that page:
To examine the effect of running this example with regedit, inspect the values of the following keys. Note that applications should avoid using Wow6432Node in hard-coded registry paths.
So, here is an example of what you could be doing instead:
static void Edit()
{
using (var root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (RegistryKey key = root.CreateSubKey("SOFTWARE\\Homebrew-Testing"))
{
key.SetValue("ServerName", "ServerName-Value");
key.SetValue("ImageServerName", "ImageServerName-Value");
}
}
Notice how you can ditch the second part of your code that deals specifically with Wow6432Node, which is recommended against in the article linked above.
The documentation for RegistryView states that if you request the 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.
I hope this helps. Best of luck.

webbrowser using ie10 c# winform

I want to force the webbrowser to use IE10 in my c# winform application.
I know there are other questions like this but i've already read a lot of them and i don't know where i'm wrong.
This is my code:
RegistryKey registrybrowser = Registry.LocalMachine.OpenSubKey
(#"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
registrybrowser.SetValue("myAppName", 0x02710, RegistryValueKind.DWord); //Even with QWord
I've tried different ways to set the value like:
registrybrowser.SetValue("myAppName", 1000, RegistryValueKind.DWord); //Even with QWord and String
registrybrowser.SetValue("myAppName", 1000); //even with 0x02710
I write it in the costructor of my main project before InitializeComponent().
I've got Admin permission set in the .manifest file
Thanks to all, BlackShawarna
EDIT: I discovered that the RegistryKey.SetValue(...); created a key in another path:
(#"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION")
even if my instruction said: Registry.LocalMachine.OpenSubKey
(#"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
I think it happens because IE10 works on 32bit mode. However I don't understand why it writes in that path even if i specified another one and, above all, why my application doesn't work even if I open Registry.LocalMachine.OpenSubKey(#"Software\Wow6432Node....");
If I run my program only in x64 mode, going to properties/build/x64, it won't write the key in my original path.
I had the same problem that my app wrote the value to "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION".
I changed LocalMachine to CurrentUser and now it works.
string executablePath = Environment.GetCommandLineArgs()[0];
string executableName = System.IO.Path.GetFileName(executablePath);
RegistryKey registrybrowser = Registry.CurrentUser.OpenSubKey
(#"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
if (registrybrowser == null)
{
RegistryKey registryFolder = Registry.CurrentUser.OpenSubKey
(#"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl", true);
registrybrowser = registryFolder.CreateSubKey("FEATURE_BROWSER_EMULATION");
}
registrybrowser.SetValue(executableName, 0x02710, RegistryValueKind.DWord);
registrybrowser.Close();
The executableName is something like "myAppName.exe"
Note: If the WebBrowser Controls inside a DLL you need to specify the hosting EXE's name whatever that might be, eg System.AppDomain.CurrentDomain.FriendlyName
FEATURE_BROWSER_EMULATION "myAppName.exe"=10000 (or 0x02710) and not 1000.
In HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
and HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
It works for me
You've got to say 'myAppName.exe' not 'myAppName'
If you have control over the page being rendered (a intranet page for instance) and also of the application that renders the page using the WebBrowser control, you can specify a meta tag in the page
<meta http-equiv="X-UA-Compatible" content="IE=10" />
and make use of the WebBrowser control as needed. You must have IE 10 on the machine.
Just in case you want to emulate other versions of IE, you could simply replace "IE=10" with "IE=EmulateIE9", "IE=EmulateIE8" etc.

C# Windows Forms does not open default browser after installation

I have a Windows Forms application (C#, NET 3.5) installed using an MSI installer.
In this application I have a button that when pressed opens a browser with a specific URL.
I use
Process.Start(url);
to open the browser.
This works fine when debugging, but after the installation it has less than optimal results. For example.
If I install it with the Just Me options selected i opens my default
browser (FF) with current settings.
If I install it with the Everyone option, when I press the button
it opens a version of IE with out any of my recent settings
(proxy, toolbars displayed etc)
As far as I can tell this issue is caused by the user associated with the application when installing.
Taking into account that may users require proxies and personal browser settings and that the Just Me, Everyone choice should remain up to the user. What is the best course o action?
I tried calling Process.Start(url) with the current logged in user using
ProcessStartInfo.UserName = Environment.UserName
But it also requires a password and asking for credentials is not an option.
Do you have any other suggestions, am I using Process.Start() incorrectly, are there settings I need to make during installation, is there anything I missed?
UPDATE:
Using Process Explorer as data_smith suggested I noticed the following:
If I install the application for Everyone it will start under the NT
AUTHORITY\SYSTEM user hence the unconfigured browser.
If I install the application with Just Me selected it starts under
the current user
Is there a way, without asking for credentials, to make the application start (at windows boot) under the current user even though it is installed for everyone?
UPDATE: Following a suggestion by data_smith to use ShellExecute and the suggestions here and here I was able to solve the problem and get the desired behavior.
The main issue was that when the installer finished the application was started with Process.Start(); This started the application as the NT AUTHORITY\SYSTEM user (the users installers run under) therefore all browsers opened by this application would also be under SYSTEM user. By using the suggestion from data_smith and the suggestions linked above I was able to start the process under the current user.
After the computer is rebooted the application starts under the correct user as this is configured through registry entries.
I recommend accessing the registry to determine the default browser.
//Create a registry key to read the default browser variable
RegistryKey reader = Registry.ClassesRoot.OpenSubKey(#"http\shell\open\command");
//Determine the default browser
string DefaultBrowser = (string)reader.GetValue("");
I tried using this code, and discovered that my registry key ended with "-- \"%1\"". I don't know why it was there, but I recommend using the following loop to ensure that the key ends in the right place.
//If the path starts with a ", it will end with a "
if (DefaultBrowser[0] == '"')
{
for (int count = 1; count < DefaultBrowser.Length; count++)
{
if (DefaultBrowser[count] == '"')
{
DefaultBrowser = DefaultBrowser.Remove(count + 1);
count = DefaultBrowser.Length + 22;
}
}
}
//Otherwise, the path will end with a ' '
else
{
for (int count = 0; count < DefaultBrowser.Length; count++)
{
if (DefaultBrowser[count] == ' ')
{
DefaultBrowser = DefaultBrowser.Remove(count + 1);
count = DefaultBrowser.Length + 22;
}
}
}
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Add a link to the LinkLabel.
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = "http://www.dotnetperls.com/";
linkLabel1.Links.Add(link);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// Send the URL to the operating system.
Process.Start(e.Link.LinkData as string);
}
}
}

Find default email client

Using C#, how can I determine which program is registered as the default email client? I don't need to launch the app, I just want to know what it is.
Use the Registry class to search the registry. This console app demonstrates the principle.
using System;
using Microsoft.Win32;
namespace RegistryTestApp
{
class Program
{
static void Main(string[] args)
{
object mailClient = Registry.GetValue(#"HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail", "", "none");
Console.WriteLine(mailClient.ToString());
}
}
}
You can look in the registry on the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail
You can read this registry key from
HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail
Default email client depends on the user. HKLM lists all registered email clients; the first one returned may not be the current user's default. Better to read HKEY_CURRENT_USER\Software\Clients\Mail.
Also this only gives you the name of the email application. If you want its executable file name, you have to go on with something like:
object mailCommand = Registry.GetValue(#"HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\" + mailClient.ToString() + #"\shell\open\command", "", "none");
and then remove anything extraneous from the command-line string that you don't need (quotes, parameters).
I think you should be able to find that info in the registry at HKLM\Software\Clients\Mail.
Look for the default string value.

Categories