打开程序一次,将多个文件作为来自explorer的参数

时间:2010-04-24 09:06:38

标签: .net vb.net explorer contextmenu single-instance

我有一个程序可以工作,使用资源管理器中的右键菜单打开文件。但是,如果我选择多个文件,然后右键单击并打开我的程序,那么它会打开我的程序的多个实例,而不是仅将多个文件作为参数传递给单个实例。该程序是用vb.net编写的,但不是windows窗体,它只是一个模块,所以我可以在Visual Studio的属性中勾选Single实例选项。

那么如何在单个实例中从资源管理器上下文菜单中打开多个文件。

3 个答案:

答案 0 :(得分:5)

这里没有满意的答案,Windows资源管理器不提供一种简单的方法来启动程序传递所有选定的文件。这需要shell context menu handler,它们很难用托管代码编写。并且无法安全地编写.NET 4.0。

使用VB.NET中提供的应用程序框架很容易模拟它,使您的应用程序成为单例并实现StartupNextInstance event。唯一的问题是这不是特别快。并且它在控制台模式应用程序中不起作用。

答案 1 :(得分:0)

虽然我知道这是针对vb.net的,但我相信你可以使用这个c#代码进行一些修改,这对我有用。也许这不是最好的方式,但对我来说,这是最简单的方法。在运行第二个副本之前,它会检查应用程序标题当前是否正在运行。

这是在Program.cs

 static frmMain Form;

    [STAThread]
    static void Main(string[] args)
    {
        bool blnCurrentlyRunning = false;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Process[] processes = Process.GetProcesses();
        foreach (var item in processes)
        {
            if (item.MainWindowTitle.IndexOf("Application Title") != -1)
                blnCurrentlyRunning = true;
        }

        if (!blnCurrentlyRunning)
        {
            Form = new frmMain();
            Application.Run(Form);
        }
        else
        {
            Application.Exit();
        }
    }

答案 2 :(得分:0)

此链接帮助我通过单击上下文菜单项获取资源管理器中所选文件的路径: .NET Shell Extensions - Shell Context Menus

它是用C#语言编写的,但正如该文章海报所说它也可以在vb.net中使用。

真的很容易:)

希望这也对你有所帮助! :)

这是步骤:

1)下载SharpShell库>>

下载文章顶部的“SharpShell Library”压缩文件,并添加对下载的SharpShell.dll文件的引用。

或者您可以通过Nuget下载:

如果您安装了Nuget,只需快速搜索SharpShell并直接安装 - 或者在https://www.nuget.org/packages/SharpShell获取包裹详情。

添加以下参考文献:

System.Windows.Forms
System.Drawing

在代码顶部使用这些代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpShell;
using SharpShell.SharpContextMenu;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using SharpShell.Attributes;

SharpContextMenu导出您的班级  右键单击该行的SharpContextMenu部分,然后选择Implement Abstract Class

CanShowMenu

调用此函数以确定是否应显示给定文件集的上下文菜单扩展。用户选择的文件位于属性SelectedItemPaths中。我们可以检查这些文件路径,看看我们是否真的要显示菜单。如果应显示菜单,请返回true。如果没有,请返回false

CreateMenu

调用此函数以实际创建上下文菜单。我们需要返回一个标准的WinForms ContextMenuStrip

这是整个命名空间SourceCode:

namespace CountLinesExtension
{
[ComVisible(true)]
[COMServerAssociation(AssociationType.ClassOfExtension, ".txt")]
public class Class1 : SharpContextMenu
{
protected override bool CanShowMenu()
{
        //  We will always show the menu.
        return true;
        //throw new NotImplementedException();
    }

    protected override ContextMenuStrip CreateMenu()
    {
        //  Create the menu strip.
        var menu = new ContextMenuStrip();

        //  Create a 'count lines' item.
        var itemCountLines = new ToolStripMenuItem
        {
            Text = "Count Lines"
        };

        //  When we click, we'll call the 'CountLines' function.
        itemCountLines.Click += (sender, args) => CountLines();

        //  Add the item to the context menu.
        menu.Items.Add(itemCountLines);

        //  Return the menu.
        return menu;
        //throw new NotImplementedException();
    }
    private void CountLines()
    {
        //  Builder for the output.
        var builder = new StringBuilder();

        //  Go through each file.
        foreach (var filePath in SelectedItemPaths)
        {
            //  Count the lines.
            builder.AppendLine(string.Format("{0} - {1} Lines",
              Path.GetFileName(filePath), File.ReadAllLines(filePath).Length));
        }

        //  Show the ouput.
        MessageBox.Show(builder.ToString());
    } 

}
}

接下来,我们必须给大会一个强有力的名字。有一些方法可以解决这个问题,但通常这是最好的方法。为此,右键单击项目并选择“属性”。然后去'签名'。选择“签署程序集”,为密钥指定“新建”并选择密钥名称。您可以根据需要使用密码保护密钥,但不是必需的

现在安装并注册Shell扩展: regasm工具

您可以使用工具'regasm'来安装和注册shell扩展。使用regasm时,shell扩展将安装到注册表中(即COM服务器的类ID将放在COM服务器类部分并与实际服务器文件的路径相关联),它还将注册关联。

服务器管理器工具

服务器管理器工具是我首选的安装/卸载和注册/取消注册的方法,至少在开发期间,因为它允许您作为单独的步骤进行安装和注册。它还允许您指定是以32位还是64位模式安装/卸载等。

这是整个Sample sourceCode。我们可以添加任意数量的上下文菜单项,任何函数,任何fileExtension等。

相关问题