Reputation: 203
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
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:
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:
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