How elevate priviledge only to access windows registry

398 Views Asked by At

I have a Visual Studio Forms application and I'm trying to implement a drag-n-drop operation from Windows Explorer into a button (or even a listbox).

But, due to some registry operations I have to perform within the code (in LOCAL_MACHINE key), I run my application ALWAYS as Administrator and it seems to invalidate any Drag-N-Drop operation from Windows Explorer into my application (I guess Windows Explorer never runs as Admin and that´s the main problem).

So, I´m actually between two paths:

1- If I put the application to run as Admin (elevated priviledges), I have no drag-n-drop from Windows Explorer but all accesses into Registry runs 100%.

2- If I put the application to run in low priviledge (no Admin), I have drag-n-drop (only to listbox, not into button) but the necessary operations into Windows Registry thrown an exception and cannot be performed.

I tried the following steps but I´m having exception in all modes if running the app in lower priviledges:

 Dim rkey As RegistryKey = Registry.LocalMachine.OpenSubKey(MyKey, True)

    Dim rkey As RegistryKey = Registry.LocalMachine.OpenSubKey(MyKey, True,
 Security.AccessControl.RegistryRights.FullControl)

    Dim rkey As RegistryKey = Registry.LocalMachine.OpenSubKey(MyKey, True, 
Security.AccessControl.RegistryRights.TakeOwnership)

Is there any way to elevate the authorization ONLY DURING THE ACCESS INTO WINDOWS REGISTRY? I´m using Visual Studio 2013 and coding in VB.

Thanks in advance for any help! Kind regards.

1

There are 1 best solutions below

1
On

You cannot change privileges to a running process. But you can start new process with elevated privileges and there do what you need to do.

You should create a small console app with code that needs elevated privileges and run this app by starting a process with elevated privileges.

It is a security thing - you don't want your app to run as Admin all the time.