2

I'm trying to implement an option in the context menu of Windows Explorer for any file and any folder. I have accomplish this by writing into regedit.

Using Microsoft.Win32;
...
RegistryKey key;
// Register to any file
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\*\shell\MyProject");
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\*\shell\MyProject\command");
// Register to folder
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\Folder\shell\MyProject");
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\CLASSES\Folder\shell\MyProject\command");
// Default value points to the app
key.SetValue("", Application.StartupPath + @"\MyProject.exe");
key.Close();

The application opens as I want, however I have no clue how to grab the path of file/folder that was selected in the context menu. How can I do this?

1
  • I guess it's provided as your first command line argument to your main method. But I'm not sure if you have to place some markup like "%1" or something in the registry "command" value. Commented Dec 20, 2015 at 13:15

2 Answers 2

1

Change the value of the registry key to

key.SetValue("", Application.StartupPath + @"\MyProject.exe %1");

So %1 is replaced with the selected file/folder. In your main method you can access this via:

static void Main(string[] args)
{
    Console.WriteLine("Selected file/folder: {0}", args[0]);
}

Unfortunatly this will not work for multi-selection. Placing %2 etc is of no use. If multiple files or folders are selected your application gets called for each of them seperatly.

Sign up to request clarification or add additional context in comments.

Comments

1

The answer of René Vogt is great only this line:

key.SetValue("", Application.StartupPath + @"\MyProject.exe %1");

should be:

key.SetValue("", Application.StartupPath + @"\MyProject.exe \"%1\"");

Without the quotation marks the args[] array contains multiple strings when the directory or file path contains whitespaces.

1 Comment

I'm using WPF C# app. I see in registery it creates the key and value. But, how do i get the current path of folder or file in WPF C# app?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.