1

I would add a new entry "Share" in Context menu, so with the right click on a file I would see this new entry "Share" in Context Menu. I tried as in the code below, but method CreateSubKey() throw th exception System.UnauthorizedAccessException saying Denied Access to key HKEY_CLASSES_ROOT

const string MenuName = "*\\shell\\NewMenuOption";
const string Command = "*\\shell\\NewMenuOption\\command";
RegistryKey key = null;
key = Registry.ClassesRoot.CreateSubKey(MenuName, RegistryKeyPermissionCheck.ReadWriteSubTree); 
key = Registry.ClassesRoot.CreateSubKey(Command, RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue("Share", ObjectToStore);

I saw something on this guide but I think that on .NET2.0 it was different

Federico Rizzo
  • 183
  • 2
  • 9
  • 1
    I Solved the probem running a separate exe with administrator rights which set key. In this way the request of right is asked only once @Michael – Federico Rizzo Oct 13 '17 at 08:00

2 Answers2

1

I think you need (local?) administrator permissions for this. Try executing your app as administrator.
If this works you have two choices:

Method one is to require administrator rights to run your application - not the best idea.
Method two would be a second (command line) project which has only one purpose, creating those registry keys. This app requires administrator permissions. And you can run this application (Process.Start(...)) from you main application.

To force admin-permissons in your application add an app.manifest / manifest-file to your visual studio project and uncomment this line <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

The hole file should be looking like this (there are more default entries in the app.manifest visual studio generates for you)

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
        <!-- <requestedExecutionLevel  level="highestAvailable" uiAccess="false" /> -->
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

Then go to project settings, application and in the lower screen under manifest you choose your app.manifest file. Rebuild. Done.

Michael
  • 1,931
  • 2
  • 8
  • 22
  • Where possible, creating applications that require elevated privileges should be avoided. In this case, adding to HK_USERS would likely be more appropriate. – DiskJunky Oct 12 '17 at 14:44
  • @FedericoRizzo Some more details would be nice... What have you tried and whats happening? – Michael Oct 12 '17 at 14:59
  • 1
    It surely *is* the better solution (in your case), but sometimes admin-permissions are required. Then this is one way to go. I've tested my post (again), works for me. – Michael Oct 12 '17 at 15:26
  • Now I understand that Context menu can be edited with HK_USER so I have to find another solution. I have to use HK_CLASSES_ROOT. I will try better your solution if I can't find nothing – Federico Rizzo Oct 12 '17 at 15:38
  • It works. I have only to understand as to avoid the permission request every time when the app is launched – Federico Rizzo Oct 12 '17 at 16:22
0

If you're adding to HK_CLASSES_ROOT, your application will have to be running with elevated privileges. You should ideally be keeping any changes to HK_USERS which should have the same sub keys

DiskJunky
  • 4,750
  • 3
  • 37
  • 66