Open program once with multiple files as arguments from explorer

I have a program that works when a file is opened using the right click menu in File Explorer. But if I select multiple files and then right click and open my program, it will open multiple instances of my program, instead of just passing multiple files as arguments to one instance. The program is written in vb.net, but it is not a window form, it is just a module, so I can check the "Single Instance" option in the Visual Studio properties.

So how do I open multiple files from the explorer context menu in one instance.

+2


a source to share


4 answers


No happy answers here, Windows Explorer does not provide an easy way to launch your program transferring all selected files. This requires a shell context menu handler and is very difficult to write in managed code. And before .NET 4.0 it was not safe to write.



However, it is easy to simulate it with an application platform, available in VB.NET, make your application and implement singlonom StartupNextInstance event , only problem is that it is not particularly fast. And it doesn't work in console applications.

+4


a source


Well this thread is 10 months old. I like to add something. For C # use this link - "C # Example"



0


a source


Although I know this was for vb.net, I'm sure you can use this C # code with a few modifications, it worked for me. This may not be the best way to do it, but it was the easiest way for me. It checks to see if the application header is currently running before starting the second copy.

This is in 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();
        }
    }

      

0


a source


this link helped me get the paths to the selected files in explorer by clicking the context menu item: .NET Shell Extensions - shell context menus

in C #, but as the poster of this article says it can also be used in vb.net.

real easy :)

Hope it helps too! :)

Here are the Steps:

1) Download SharpShell library ->

Download the "SharpShell Library" zip file at the top of the article and add a link to the downloaded SharpShell.dll file.

or you can download it via Nuget:

If you have Nuget installed, just do a quick search for SharpShell and install it directly - or get package details at https://www.nuget.org/packages/SharpShell .

Add the following links:

System.Windows.Forms
System.Drawing

      

Use them at the top of your code:

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;

      

Derive your class from SharpContextMenu

 Right click on the SharpContextMenu part of the line and select Implement Abstract Class

.

CanShowMenu

This function is called to determine whether to show the context menu extension for a given set of files. The files selected by the user are in the property SelectedItemPaths

. We can check these file paths to see if we really want to display the menu. If the menu should be shown, return true

. If not, return false

.

CreateMenu

This function is called to actually create the context menu. Standard WinForms ContextMenuStrip

is all we need to return.

Here's the complete SourceCode namespace:

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());
    } 

}
}

      

We then need to give the assembly a strong name. There are ways to work around this requirement, but this is generally the best approach. To do this, right-click on the project and select Properties. Then go to the "Signing" section. Select Sign the Assembly, select Generate for the key, and choose a name for the key. You can password protect the key if you like, but this is not required.

Now install and register the shell extension: regasm tool

You can use the regasm tool to install and register a shell extension. When using regasm, the wrapper extension will be installed in the registry (that is, the COM server class ID will be placed in the COM Server Classes section and linked to the path to the actual server file), it will also register the associations.

Server Manager Tool

The Server Manager Tool is my preferred approach for install / uninstall and register / register, at least during development, as it allows you to install and register individual steps. It will also let you tell you if you install / uninstall, etc. In 32-bit or 64-bit mode.

This was the entire Sample sourceCode. we can add any number of context menu items, any function, any fileExtension, etc.

0


a source







All Articles