Delete Folder from Registry - Permission Issues
Asked Answered
F

3

1

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?

Furred answered 3/1, 2012 at 14:47 Comment(4)
#2022331 , refer to this Question ,the answer told him how to give a single Method Admin Right's .Renown
here is a great article to read on understanding Windows Registry Files and Folders - -msdn.microsoft.com/en-us/magazine/cc982153.aspxDeese
i do have admin rights on my computer.Do i still need to exclusively add the rights using the code. I would prefer doing it using code rather than manual methods.Furred
@Sangram - Your user account might have administrator rights that doesn't meant the program does.Lepidolite
L
4

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.

Lever answered 3/1, 2012 at 14:57 Comment(2)
I think the first (with one extra boolean parameter) should be enough for your case.Lever
You can use the first one but you can still have UAC problem. So combine with manifest way. To be able to debug, run VS as administrator.Mortmain
F
1

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)

Fascia answered 3/1, 2012 at 15:0 Comment(3)
so i tried editing manifest file & changed code to `<requestedExecutionLevel level="highestAvailable" uiAccess="true"/>' is it sufficient ?Furred
C-F's answer helped & worked for me. Thanks anyways for help.Furred
for my application it is necessary to have admin rights. So i didn't check it.Furred
R
1

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!

Rebound answered 3/1, 2012 at 15:31 Comment(3)
I have edited the manifest files & tried the same code but it is giving me same exception "Cannot write to the registry key." any guesses ? btw every time i run/debug the code it sets the manifest files to the default values..Furred
When you say manifest files do you mean you have more than one?Rebound
I don't think it should appear in there. I have just created a test project in VS and it appears in Projects\Projectname\Projectname. Did you add it to VS through the setup screen or manually? It should show up as app.manifestRebound

© 2022 - 2024 — McMap. All rights reserved.