views:

56

answers:

1

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?

+1  A: 

After some research I discovered that write permissions may be needed so using the overload for 2nd param will allow you write perms.

Your line:

...OpenSubKey(@"Software\Clients\StartMenuInternet",true)...

http://msdn.microsoft.com/en-us/library/xthy8s8d.aspx

RobertPitt
Why do you need to escape registry paths twice (`@` and `"\\"`)?
dtb
you don't, Im not a superuser at C#, just dabble little apps here and there, and i never knew about the `@` for escaping, check my update to see if that helps.
RobertPitt
That was it! I can't believe I missed that, thanks. :-)
Rytmis