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

发布于 2024-09-24 23:15:20 字数 411 浏览 0 评论 0原文

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

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

我的程序需要支持什么才能使用此机制? 我的程序可能需要对 Windows 执行什么操作才能启用上下文菜单支持?

我想知道是否有人可以指出我关于这个主题的好文章/教程的方向。

Possible Duplicate:
Filetype association with application (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.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巡山小妖精 2024-10-01 23:15:20

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

自动将程序放入打开方式列表中,您需要在 HKEY_CLASSES_ROOT\YOUR_EXT 中添加一些注册表项\。 这是一个答案说明如何做到这一点

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

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

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 answer saying how to do it

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

々眼睛长脚气 2024-10-01 23:15:20

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

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

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

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

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.

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:

  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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文