Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Shell上下文菜单_C#_Contextmenu_Windows Shell - Fatal编程技术网

C# Shell上下文菜单

C# Shell上下文菜单,c#,contextmenu,windows-shell,C#,Contextmenu,Windows Shell,我目前正在运行一台Windows7x64计算机 我编写了以下代码,以便在右键单击时添加上下文菜单: RegistryKey rKey = Registry.ClassesRoot.OpenSubKey("Directory\\Background\\shell", true); String[] names = rKey.GetSubKeyNames(); foreach (String s in names) { System.Windows.F

我目前正在运行一台
Windows7x64计算机

我编写了以下代码,以便在右键单击时添加上下文菜单:

    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();                  
如果我直接在注册表上重复这个过程,它会工作,但不是通过这个


我还可以访问注册表,因为我添加了一个代码段,其中列出了我需要的所有子项,但没有添加任何子项。

我已经测试了您的代码,它很好。看起来您没有从代码中打开注册表的访问权限。只需遵循以下简单步骤:

  • 关闭您的VisualStudio。然后以管理员模式再次打开它。您可以通过右键单击VisualStudio链接并选择“以管理员身份运行”选项来执行此操作
  • 打开您的代码并从那里运行它
  • 如果要直接从Exe运行程序,请右键单击Exe并选择“以管理员身份运行”选项

    如果不想以管理员身份运行,请执行以下步骤:

  • 向项目中添加一个名为App.manifest的新文件;通过从Project中添加新文件
  • 将以下数据添加到该文件中,rest将发挥神奇的作用
  • 只需将您的应用程序名称替换为MyApplication.app。重要的部分是本节。Rest是自动生成的

    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">
    

    我已经测试了你的代码,它很好。看起来您没有从代码中打开注册表的访问权限。只需遵循以下简单步骤:

  • 关闭您的VisualStudio。然后以管理员模式再次打开它。您可以通过右键单击VisualStudio链接并选择“以管理员身份运行”选项来执行此操作
  • 打开您的代码并从那里运行它
  • 如果要直接从Exe运行程序,请右键单击Exe并选择“以管理员身份运行”选项

    如果不想以管理员身份运行,请执行以下步骤:

  • 向项目中添加一个名为App.manifest的新文件;通过从Project中添加新文件
  • 将以下数据添加到该文件中,rest将发挥神奇的作用
  • 只需将您的应用程序名称替换为MyApplication.app。重要的部分是本节。Rest是自动生成的

    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">
    

    谢谢,成功了。比如说,我将其绑定到一个
    .exe
    ,让用户在他们的系统上创建类似的菜单。我怎样才能“以管理员身份运行”呢?谢谢,这很有效。比如说,我将其绑定到一个
    .exe
    ,让用户在他们的系统上创建类似的菜单。我如何能够动态“以管理员身份运行”?
      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>