Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi!

I'm trying to start an c# application just form a right click context menu in windows and when the application will open i want to fill a TextBox with the path of the folder I made that action, like in the picture:
https://plus.google.com/u/0/photos/105606022912012406932/albums/5670323163492912417/5670323163285239298

I want not to write manuale in the textbox the path of the folder, to complete automatic.

Thanks!
Posted
Updated 1-Nov-11 23:41pm
v2
Comments
[no name] 2-Nov-11 13:44pm    
Your link is broke.

1 solution

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:

C#
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

C#
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}


to

C#
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

C#
public Form1()
{
}


to

C#
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:
C#
textBox1.Text = folderPath;


Note: if this app will be deployed as a clickonce app you will need to use:
C#
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900