Question

The exercise is to start a C# programm from the explorers context menu and the programm should know the path from the folder on with the context menu was opened.

What I did so fare: Add my programm to the explorers context menu and add this code:

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

string filename;
ArrayList windows = new ArrayList();

foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
    filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
    if (filename.Equals("explorer"))
    {
        Console.WriteLine("Hard Drive: {0}", ie.LocationURL);
        windows.Add(ie);

        Shell shell = new Shell32.Shell();
        foreach (SHDocVw.InternetExplorerMedium sw in shell.Windows())
        {
            Console.WriteLine(sw.LocationURL);
        }

    }
}

Basicaly this did work but only for the folder which is selected on the left tree view in explorer. And the next problem is when I use sw.LocationURL with the path "C:\C#" the result is "file:///C:/C%23".

Is there a simpler way to get the folder properties(path, name) on which I open the context menu to c#?

Was it helpful?

Solution

I can't speak to the first part of your question, because I haven't mucked around with shell extensions. But file:///C:/C%23 is the correct Url for the directory C:\C#, with urlencoding applied. If you want to turn this string into a filesystem path, you can do something like this:

var uri = new Uri("file:///C:/C%23");
var path = Uri.LocalPath;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top