I am currently working on a simple C# Windows Forms application to help me with editing/authoring articles for CodeProject, that basically strips unwanted files off zip files, and I wanted to associate an Explorer context menu entry with zip files that’d open the zip in my application. Of course, this was a mere matter of editing the registry and adding an odd key there and an odd value here. But I thought the function would be pretty useful for other people who might want similar functionality and so wrote it generically.
Usage
Here is how you can use the function :-
AddContextMenuItem(".zip", "ZipStrip",
"Open with &ZipStrip", Application.ExecutablePath + " %1");
Code Listing
I had to mess with the indentation to avoid horizontal scrolling π
//Extension - Extension of the file (.zip, .txt etc.)
//MenuName - Name for the menu item (Play, Open etc.)
//MenuDescription - The actual text that will be shown
//MenuCommand - Path to executable
private bool AddContextMenuItem(string Extension,
string MenuName, string MenuDescription, string MenuCommand)
{
bool ret = false;
RegistryKey rkey =
Registry.ClassesRoot.OpenSubKey(Extension);
if(rkey != null)
{
string extstring = rkey.GetValue("").ToString();
rkey.Close();
if( extstring != null )
{
if( extstring.Length > 0 )
{
rkey = Registry.ClassesRoot.OpenSubKey(
extstring,true);
if(rkey != null)
{
string strkey = "shell\\" + MenuName + "\\command";
RegistryKey subky = rkey.CreateSubKey(strkey);
if(subky != null)
{
subky.SetValue("",MenuCommand);
subky.Close();
subky = rkey.OpenSubKey("shell\\" +
MenuName, true);
if(subky != null)
{
subky.SetValue("",MenuDescription);
subky.Close();
}
ret = true;
}
rkey.Close();
}
}
}
}
return ret;
}
Nish, I’m sure you are psychic. I was just planning out my next app when I decided to add explorer menu items. I hate registry stuff as finding anything in it is a nightmare. You just saved me at least an hours programming. Cheers.
Glad to be of help, Nonny π
BTW be sure to check the return value (it will be
trueon success), because if you don’t have write-rights to HKCR, the function will fail.Regards
Nish
You should have quotes around the %1 part, if you leave those out you might have problems when you encounter a filename that contains spaces.
Thanks for pointing that out Mike.
Nish
Your feed still looks broken from sharp reader.
hi nish,
Iam a aptech student in from fiji and currently facing some problems with c#, could you please e-mail me some source codes explaining storing values and manupulating it using stuctures.
Thank You
Nice code, congratulations!
If I place “.bmp” in extension and I select 20 bmp files in explorer and execute the command, the explorer will open 20 instances of my program. How I do to these 20 bmp files will be, instead, 20 args of the command line???
Regards
This looks great. One question I have is how would I go about getting file names with spaces? (I’ve tried putting quotes around the %1). What it is doing is actually passing the old DOS filename (c:\blah\blahh~\blah~.ZIP) to my program. If I use the FileInfo object and do a MessageBox.Show(fi.Name) it shows the DOS filename convention. My program actually parses the file name, so I would need some way to get that from this.
Thanks!!
This helped me a lot. Just one doubt. How do I enable/disable a context menu. I want my context menu to appear only if something is copied to the clipboard. By something I mean Folder(s).
Please help me. How to add menu item with icon?
I like your code it helped a lot with registry editing, but adds nothing to the context menu, any directions?
Instead of messing with the indentation, you might have replaced “if(!null) {}” with “if(null) return false;”
Just 2 cents π
Hi! I was surfing and found your blog post… nice! I love your blog. π Cheers! Sandra. R.
I love your site. π Love design!!! I just came across your blog and wanted to say that IΒve really enjoyed browsing your blog posts. Sign: ndsam
That was a good article nish. I need further enhancement in my project that is I must add icon to the menu item. How can I do that.
Thanks in advance.
what if i wanna have 2 or more different program with the same MenuName? eg, *.doc, *.txt, *.jpg send into the function..?
thank u very much
Nice stuff man.. Keep up the good work. found it useful..
# re: Pivot function (C#)
Whats Up,
What a great SIMPLE way to add a menu dude!
I’v thought about this-but never wrote it-and why should I?
YOU ALREADY DID π
Thanks!
Why pay for libraries when You have Nish for free? !
Data mining and Text mining with VB.NET. Visual Studio .NET .ADO.NET. Programming Services
Hiring / VB.NET Jobs
Domain Investor Needed
i used the same code but it worked only for folders and not any other file say abc.txt etc…
GShaik and Nish, would you please show how an icon(transparent) can be added at the left side of the added text like WinRAR
If my program is started by the context menu – as far as I can tell no commandline argument is sent to the app!! So how can I make use of this?!
Thanks for the post anyway!
Thanks Nish, It’s exaclty what I needed.
bookmaked you’re blog… lots of interesting stuff
btw… another method for removing Explorer context menu could be helpful too π
Thanks for the simple but very useful code!
A tip: surround the MenuCommand with quotation marks to allow the application to open files containing spaces — in your example it should be like this:
“\”” +Application.ExecutablePath + “\” “\%1\””
thanks for the tutorial. but when I click on my option in the MENU, I receive an error. how can I do to Launch my application after the click?
Hi Guys,
This post was very useful for me and i even made a tool for easy folder sharing. It can be found in the codeplex in the below link.
http://easyshare.codeplex.com/
Please try it and leave feedback.
MJ
a great tutorial. however, in function main (string [] args) is still missing save path parameter. I do not know how to do this? My goal is to add my program into ContextMenu (right click on excel file when my program and select the program will process excel file)
Hi guys, I have a problem, what I can do if a need the path where I call the .exe from the right click of a .txt for a example ?
I need the path of the .txt
How to add sub menus to it like that of “New” or “Arrange Icons By” that are by default in explorer.
The Windows Forms Menu Controls offers much more amazing navigation menus than the classic Office style menus and can fully customize item appearance, text, orientation, image, checkbox, and any other controls at design time
Great! Thanks!
Hi, this is great but how does the application know the file that was clicked when it opens?