Question

I have a written a simple C# WPF application that helps renaming batches of files in the particular way I need in my company (in several various optional patterns - add prefixes, remove prefixes, replace some characters etc).

It all works pretty well, but not always do I want to actually start the app, navigate to a folder etc, so I would like to add one particular function of the program (remove prefix) as a shortcut to the Windows Context Menu.

Basically, what I would want to achieve is the ability to select a number of files in explorer, right click them, choose "remove prefix" and have them renamed - I don't want the app window to appear if it's not necessary (though I can live with it being active as minimized to tray).

I know more or less about adding context menu items to registry (with command line parameters), but I am not sure whether it would work for me, since I just need a particular function, not the whole application.

Any ideas how should I do it?

Was it helpful?

Solution

Add a command line option to your program which runs just that function.

override OnStartup in App.xaml.cs and use StartupEventArgs.Args to get the command line arguments.

Alternatively if your function doesn't need the ui to run you can replace Main, add Program.cs and add

class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
        if (runNormally(args))
        {
            MainWindow mainWindow = new MainWindow();
            var app = new Application();
            app.Run(mainWindow);
        }
        else
        {
            MyFunction(args);
        }
    }
}

Register the context menu so that it passes the command line arguments to run the correct function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top