Chris Jones
Chris Jones

Reputation: 2746

Determine if a user has permissions to edit the registry?

I'm writing a program that edits the registry using c#

I would like to check if the user has access to edit when the program loads. What is the best way to check in c#?

Upvotes: 0

Views: 3717

Answers (4)

Nate
Nate

Reputation: 13242

try{
    Registry.CurrentUser.OpenSubKey(@"PATH\TO\STUFF", true);
    // Have write permissions.
}
catch {
    // Do not have write permissions.
}

Upvotes: 1

Steve B
Steve B

Reputation: 37660

Each node in the registry has its own ACL (Access Credential List). There is not a single right.

Commonly, each application will have its own registry node, either in the HKLM hive or in HKCU hive, or a combination of both.

In the former case, the user has to be administrator of the computer, in the latter the user can read/write its own registry.

To check the actual access permission, you can use the RegistryKey.GetAccessControl method.

However, as stated, you have to have at least the Read permissions to call this method.

Upvotes: 2

nyxthulhu
nyxthulhu

Reputation: 9752

A user shouldn't have access to edit "the full registry" they should only have permissions to edit certain keys within the registry.

You would be best doing a try catch system

try{
     // Do registry edit
} 
catch {
     Console.Log("Sorry you don't have permission to edit this key");
}

Each user is different and can have access to some parts, all parts or no parts of the registry using the windows ACL.

Have a peek at the first answer here.

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 34013

Try to open a RegistryKey with write access. If it gives you an Exception, you don't have permission. You could probably even specifiy which Exception to a certain one which says you don't have permission.

Upvotes: 1

Related Questions