开发者

Shell Integrate in Windows for a Specific File Type With C#

So I searched for a guide of how to shell integrate your application (add it to the right click menu) with C#, but I couldn't find how to do that only for a specific file type. I know it is possible because WinRar does that. So how can 开发者_运维百科I do that?


There are usually two-ish ways you can implement this.

  1. Registry Keys - You can write keys and values under HKEY_CLASSES_ROOT. If you look at that hive you'll see the extensions on your pc. Look at this article for the details about the keys and values. Something simple like an option to open .myfile types with your application is possible here. Here is a File Association Example

  2. Shell Extensions (Written in COM) : Here you can do more complicated stuff like Handlers. They will get called by Windows so you can do things like paint on menus, or add custom actions when a file is right-clicked. There is more to it here than files, you can even add property sheets and custom tooltips.

You will find some talk about not using .NET to write a Shell Handler**. This applies only to older versions of .NET. Its all ok with .NET4.

This article should help you with a Context Menu Handler in .NET4

** Why was it not recommend:

When you write a shell handler, it gets called by the host process (typically windows explorer), but also things like FileOpenDialogs and FolderBrowser dialogs. So a problem would occur if you wrote a shell extension in .NET 2.0, and a .NET 1.1 app called a File Open Dialog and then your .NET 2.0 shell handler would be called into and your .NET 1.1 app which has an older CLR and there would be version conflict.

So I'm pleased to have found out finally this has been fixed somehow in .NET 4 =)


Windows Explorer right click menus are controlled by the registry. Specifically, the HKEY_CLASSES_ROOT hive.

A good way to get a good idea how everything works in there is to check out HKCR\.txt which shows what will happen for text files in the right click menu. Look at the (Default) key, which points to "txtfile". HKCR\txtfile will then have a subkey HKCR\txtfile\shell\open\command. The (Default) key for that shows the command to open notepad.exe with a parameter of "%1", which indicates the file being opened. Replace the open key with some other name (see print and printto keys in the txtfile key) to add a different custom command to the right-click menu.

Once you get a grasp on what you need to add to integrate your application, you can check out the Microsoft.Win32 namespace for classes to help manipulate the registry via c# code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜