I'm designing a C# WinForms program that when the user right clicks on a directory and selects the item which I added to shell context menu (which opens the .exe for my application), it runs in the background based on where the user right clicks.
I've already figured out how to install it and add it to the correct context menu, but I can't seem to figure out the most crucial part of the program. I've already looked here, but that doesn't answer my question and the answer it gives just leads to another question.
I also realize that command line arguments exist, and that is how this question is answered. When I go onto Microsoft's website about using command line arguments, it is only about using an actual command line, which I am not using.
So my question is:
How exactly do I get the directory path when a user right clicks a folder and choose the shell context menu which I added?
If I have to use a command line in the background, that is fine I just need to be able to get and send the directory path to my program.
Here is relevant code for how I use the entered directory. In essence source is the directory path that I want when the user right clicks.
private void recursiveCheck(string source)
{
string[] directories = Directory.GetDirectories(source);
foreach(string directory in directories)
{
string test = new DirectoryInfo(directory).Name;
if (test.Length >= 3 && (test.Substring(test.Length - 3).Equals("val", StringComparison.InvariantCultureIgnoreCase) || (test.Substring(test.Length - 3).Equals("ash", StringComparison.InvariantCultureIgnoreCase)))
{
if (Directory.Exists(directory + "\\STARTUP"))
testing_dir(directory);
else
{
MessageBox.Show("Error! Startup folder does not exist in: " + test);
Application.Exit();
}
}
else
recursiveCheck(directory);
}
}
I assume you have added your application to the context menu of folders in registry:
HKEY_CLASSES_ROOT
Directory
shell
OpenWithMyApp → (Default): Open With My App
command → (Default): "c:\myapp.exe" "%V"
The key point is in %V
. It will be the folder name which you right clicked on it and it will be passed to your application as command line argument.
Then in your application, it's enough to have something like this:
[STAThread]
static void Main()
{
string folderName = null ;
if (Environment.GetCommandLineArgs().Length > 1)
folderName = Environment.GetCommandLineArgs()[1];
MessageBox.Show(folderName);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new Form1());
}