C# Windows“打开方式>”上下文菜单行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3788429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:18:19  来源:igfitidea点击:

C# Windows 'Open With >' Context menu behaviour

c#windowscontextmenuopen-with

提问by TK.

Possible Duplicate:
Filetype association with application (C#)

可能的重复:
与应用程序的文件类型关联 (C#)

I'm writing a C# Windows app to visualise and modify '.build' files (nant scripts). I would like the user to be able to right click on a .build file in windows explorer and select the 'Open With >' option to allow the file to be modified in my app.

我正在编写一个 C# Windows 应用程序来可视化和修改“.build”文件(nant 脚本)。我希望用户能够在 Windows 资源管理器中右键单击 .build 文件并选择“打开方式>”选项以允许在我的应用程序中修改文件。

What does my program need to support in-order to work with this mechanism? What might my program need to do to Windows to enable context menu support?

我的程序需要支持什么才能使用这种机制?我的程序可能需要对 Windows 做什么才能启用上下文菜单支持?

I was wondering if anyone could point me in the direction of a good article/tutorial on this subject.

我想知道是否有人可以为我指明有关此主题的好文章/教程的方向。

采纳答案by Scott Chamberlain

The Open With command just passes the path of the file as the first argument to the application so all you need to do is

Open With 命令只是将文件的路径作为第一个参数传递给应用程序,因此您需要做的就是

public static void Main(string[] args)
{
    if(args[0] != null)
    {
       //args[0] contans a path to the file do whatever you need to do to display it
    }
    else
    {
       //Start normally
    }
}

To automaticly put your program in the open with list you will need to add some reg keys in HKEY_CLASSES_ROOT\YOUR_EXT\. Here is a SO answersaying how to do it

要自动将您的程序放入打开列表中,您需要在HKEY_CLASSES_ROOT\YOUR_EXT\. 这是一个 SO 答案,说明如何去做

Or you could just add it by hand to the open with list the normal way.

或者您可以以正常方式手动将其添加到打开的列表中。

回答by KeithS

Take a look at this blog post: Shell Extensions - Context Menu. It has code for a simple "wrapper" to some COM hooks to the Windows shell context menu. Put it in the GAC and when you right-click, your menu will be included as a sub-menu of the right-click context menu.

看看这篇博文:Shell Extensions - Context Menu。它有一个简单的“包装器”代码,用于一些 COM 挂钩到 Windows shell 上下文菜单。将它放在 GAC 中,当您右键单击时,您的菜单将作为右键单击上下文菜单的子菜单包含在内。

As far as strictly using "Open With..." to make your application show up ONLY for files it can open, that's a little easier. These are managed by Windows using registry keys in two places in the registry:

至于严格使用“打开方式...”使您的应用程序仅显示它可以打开的文件,那就更容易了。这些由 Windows 使用注册表中两个位置的注册表项进行管理:

  1. HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ FileExts \ .FileExtension \ OpenWithList (install for current user)
  2. HKEY_CLASSES_ROOT \ .FileExtension \ OpenWithList (install for all users)
  1. HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ FileExts \ .FileExtension \ OpenWithList(为当前用户安装)
  2. HKEY_CLASSES_ROOT \ .FileExtension \ OpenWithList(为所有用户安装)

Take a look at some of the existing ones using regedit, then use the Registry class to create a new key for the extension you want.

使用 regedit 查看一些现有的,然后使用 Registry 类为您想要的扩展创建一个新键。