Ok so here is what I found out by looking in the registry at how VLC media player does this.
First you will need to create a registry key. If you want the context menu entry to be available for the current user only use Registry.CurrentUser as the root key else if you want the context menu entry to be available for all users use Registry.LocalMachine as the root key. Ok so here is how to add the registry entry:
RegistryKey rootKey = Registry.CurrentUser;
RegistryKey subKey = rootKey.CreateSubKey("Software\\Classes\\Directory\\shell\\OpenWithMyApp");
subKey.SetValue("", "Open With My App");
subKey.CreateSubKey("command").SetValue("", Application.ExecutablePath + " %1");
subKey.Close();
rootKey.Close();
Ok now to retrieve the directory path in your application:
In your applications Program.cs file change your main method from
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
to
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args));
}
the in your applications main form modify your constructor from
public Form1()
{
}
to
public Form1(string[] args)
{
string folderPath = args[0];
}
And as far as populating the text box with the folder path just assign folder path to the text box. For example:
textBox1.Text = folderPath;
Note: if this app will be deployed as a clickonce app you will need to use:
string[] args = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;
instead of string[] args to retrieve your apps arguments.
And of course modify it to work for you.
Also the best way to add the registry entry is through the use of a setup project that way the entry will be added when the app is installed and removed when the app is uninstalled.
I hope this will help you (or someone) Please post a comment and let me know how it worked out for you.