Calling a C# executable from windows explorer shell context menu

I have written a simple application in C# (2005 Express Edition) / Windows Forms that displays diskspace usage for a given file/folder as a pie chart that I wish to access from the windows explorer context menu.

I have managed to add an item to the context menu for all folders using the file types tab in the folder options dialog within windows. This is sufficient for me as I don't intend to distribute this application, it is only for my own personal use, so adding it manually is OK.

My problem is that I don't know how to find out which folder was right-clicked from within my application. Is the folder name simply passed as a parameter to the application (the same as if it were called from the command line) and if so how do you access these parameters from a .Net application. I'm quite new to .Net so I'm sure it's an easy answer.

Thanks

Mog



Answer this question

Calling a C# executable from windows explorer shell context menu

  • traimo

     Mattias Sjogren wrote:

    Write a parameterized Main method

    static void Main(string[] args)

    or call System.Environment.GetCommandLineArgs().

     

    Thanks this was almost exactly what I needed.

    It seems that the pathnames passed through as the argument often contains spaces so GetCommandLineArgs() will split this into multiple arguments. Instead I ended up using System.Environment.CommandLine instead and then parsed the string myself to extract the path from it. It appears that the executable's path/filename is always enclosed by "s so I was able to search for the second ", skip the whitespace and then the rest is the path that I want.

    The System.Environment looks to be a useful class and was exactly what I was looking for in the documents but I couldn't find it.

    Thanks

    Mog


  • pjella

    Theoretically I suppose you could create a native image using NGen to get around the CLR version problem and thus write your shell extensions in managed code.

    I hasten to add this is NGen with .NET Framework v2. The version shipped with 1.0 and 1.1 had a fair few problems.



  • greeniguana

    William, I don't see how NGen is a solution here. The component still requires the appropriate CLR version to load.



  • Phil Borg

    My mistake I meant to say Salamander instead of NGen which now supports .NET v2.

  • Josh Free

  • Kumarsamy

    Mog0 wrote:

    how do you access these parameters from a .Net application.

    Write a parameterized Main method

    static void Main(string[] args)

    or call System.Environment.GetCommandLineArgs().



  • samnas

    tribal, it's not recommended to write shell extensions in managed code. See this page to learn why

    http://blogs.msdn.com/junfeng/archive/2005/11/18/494572.aspx



  • Calling a C# executable from windows explorer shell context menu