user2053912
user2053912

Reputation: 203

Shell Context Menus

I am currently running a Windows 7 x64 machine.

I have written the following code to add a context menu on right click:

    RegistryKey rKey = Registry.ClassesRoot.OpenSubKey("Directory\\Background\\shell", true);
    String[] names = rKey.GetSubKeyNames();
    foreach (String s in names)
    {
        System.Windows.Forms.MessageBox.Show(s); 
    }
    RegistryKey newKey = rKey.CreateSubKey("Your Application");
    RegistryKey newSubKey = newKey.CreateSubKey("command");
    newSubKey.SetValue("", "C:\\Windows\\System32\\notepad.exe");
    newSubKey.Close();
    newKey.Close();
    rKey.Close();                  

If I repeat the procedure directly on the registry, it works, but not via this.

I am also able to access the registry, as I have added a snippet that tells lists all subkeys that I require, but simply does not add one.

Upvotes: 0

Views: 995

Answers (1)

Santosh Panda
Santosh Panda

Reputation: 7341

I have tested your code and it is well & good. Looks like you dont have access rights to open the registry from the code. Just follow these simple steps:

  1. Close your Visual studio. Then Open it again as Run As Administrator mode. This one you can do by Right clicking on the Visual Studio link and choose Run As Administrator option.
  2. Open your code and run it from there.

If you want to directly run the program from the Exe then Right click on Exe & choose Run As Administrator option.

If you don't want to do Run As Administrator, then follow these steps:

  1. Add a new file to you project called App.manifest; by adding a new File from Project.
  2. Add following data to that file, rest it will do the magic.

Just replace your application name with MyApplication.app. The important part is the section. Rest is auto generated.

  xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
  xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <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" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

Upvotes: 5

Related Questions