I've written an application that launches browsers based on the provided URL -- the idea is, that I've got certain sites that I'd like to automatically open in IE, while the rest should open in my default browser. The thing works beautifully, so now I'm trying to register it as a browser so I can set it as my default. I'm trying to do it via the application itself.
The task is to write to HKLM\Software\Clients\StartMenuInternet
(as documented here), but it keeps failing. My application performs the following trick to elevate itself to admin using UAC:
private static void EnsureElevatedInstall()
{
using (var wi = WindowsIdentity.GetCurrent())
{
var wp = new WindowsPrincipal(wi);
if (wp.IsInRole(WindowsBuiltInRole.Administrator)) return;
using (var currentProc = Process.GetCurrentProcess())
using (var proc = new Process())
{
proc.StartInfo.Verb = "runas";
proc.StartInfo.FileName = currentProc.MainModule.FileName;
proc.StartInfo.Arguments = "/reinstall";
proc.Start();
}
Environment.Exit(0);
}
}
This works to ensure that the app runs as an admin. I get the UAC prompt when starting the process. But then, when it's actually time to write to the registry, I get an exception saying I don't have the necessary permissions.
using (var registryKey =
Registry.LocalMachine.OpenSubKey(@"Software\Clients\StartMenuInternet"))
{
const string launcherExecutable = "MyApp.exe";
using(var launcherKey = registryKey.CreateSubKey(launcherExecutable))
{
// I never get here, because CreateSubKey fails
}
}
What gives?