简体   繁体   中英

Giving Application Rights to Change Registry in Windows XP

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.Show("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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM