Shell上下文菜单

use*_*912 0 c# contextmenu windows-shell

我目前正在运行Windows 7 x64 machine.

我编写了以下代码来右键单击添加上下文菜单:

    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();                  
Run Code Online (Sandbox Code Playgroud)

如果我直接在注册表上重复该过程,它可以工作,但不是通过这个.

我也能够访问注册表,因为我添加了一个片段,告诉列出我需要的所有子键,但根本不添加一个.

San*_*nda 5

我已经测试了你的代码,它非常好.看起来你没有从代码中打开注册表的访问权限.只需按照以下简单步骤:

  1. 关闭Visual Studio.然后再以"以管理员身份运行"模式打开它.您可以通过右键单击Visual Studio链接并选择"以管理员身份运行"选项来执行此操作.
  2. 打开代码并从那里运行它.

如果要直接从Exe运行程序,请右键单击Exe并选择Run As Administrator选项.

如果您不想以管理员身份运行,请按照下列步骤操作:

  1. 在项目中添加一个名为App.manifest的新文件; 通过从Project添加新文件.
  2. 将以下数据添加到该文件中,休息它将发挥神奇作用.

只需用MyApplication.app替换您的应用程序名称即可.重要的部分是该部分.休息是自动生成的.

  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>
Run Code Online (Sandbox Code Playgroud)