Sangram Nandkhile
Sangram Nandkhile

Reputation: 18182

Delete Folder from Registry - Permission Issues

I am trying to delete one folder from registry. Let's say I want to delete the folder

Software\TeamViewer

I have written code but it is gives an exception "you can't write". I guess its some kind of problem with the Permission & access rights.

string keyapath = @"Software\TeamViewer";
RegistryKey regKeyAppRoot = Registry.CurrentUser.OpenSubKey(keyapath);
regKeyAppRoot.DeleteSubKeyTree(keyapath);

How do I give permission to my software to delete folders from registry?

EDIT: I have admin rights of my system. Do I still need to exclusively assign rights to the application through my code?

Upvotes: 1

Views: 3386

Answers (3)

Christoph
Christoph

Reputation: 448

Your application needs admin rights to change data inside the registry. To gain these rights, your application's mainfest needs to contain some values which tells windows that the application needs more rights.

Google uac .net or uac c# (UAC = User Account Control)

Or just take a look at this article.

Create and Embed an Application Manifest (UAC)

Upvotes: 1

C-F
C-F

Reputation: 1768

The OpenSubKey method with one parameter opens the key for reading. Use other variant of OpenSubKey method:

OpenSubKey(String, Boolean) -- Pass true for a second parameter to open the key with generic write access

OpenSubKey(String, RegistryKeyPermissionCheck) -- Allows some precise control over the permission chacking for subkeys

OpenSubKey(String, RegistryKeyPermissionCheck, RegistryRights) -- As above, but you can exactly specify needed rights.

See MSDN for details.

Upvotes: 4

Bali C
Bali C

Reputation: 31231

You should create a manifest file in your project (right click on your project, add new item, manifest file). Then open it, inside you will see this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> // Put this to invoke UAC for admin rights
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

Then when you run your app it will prompt you for uac, then the program will be running as administrator, hopefully giving you the access you need.

Hope this helps!

Upvotes: 1

Related Questions