开发者

Giving Application Rights to Change Registry in Windows XP

开发者 https://www.devze.com 2023-03-05 16:56 出处:网络
I don\'t have XP on any of my machines.My efforts to get a small application to run as Administrator in Vista and Windows 7 work.However one of my testers is reporting that on Windows XP he is getting

I don't have XP on any of my machines. My efforts to get a small application to run as Administrator in Vista and Windows 7 work. However one of my testers is reporting that on Windows XP he is getting the following exception:

System.UnauthorizedAccessException: Access to the registry key 'HKEY_CLASSES_ROOT\.ad2' is denied.
   at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)

The code I am using to run the application that tries to write to the registry is:

var proc = new ProcessStartInfo {
                UseShellExecute = true,
                WorkingDirectory = Environment.CurrentDirectory,
                FileName = Path.Combine(Application.StartupPath, "ADEFileAssociator.exe"),
                Verb = "runas"
            };

            try {
                Process.Start(proc);
            }
            catch {
                MessageBox.Sho开发者_C百科w("Failed to start File Associator", "Process Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

This works fine for Vista and Win7 opening the UAC dialog. If the user accepts to run then the registry is updated on these versions. I assumed that the same would work for XP. Clearly I was wrong. So my question is 'what else do I need to do to get my app to write to the registry in XP?


For XP you don't have UAC and so you can't use the runas verb. Instead the best you can do is set the UserName and Password properties of ProcessStartInfo. This pretty much sucks because you'd have to show an authentication dialog or hard code the values!

If I were in your position I'd look very hard for a way to can avoid having to gain admin rights? If that fails then I'd probably make it a pre-condition for your app to run on XP that the user had admin rights.

0

精彩评论

暂无评论...
验证码 换一张
取 消