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

孤者浪人 提交于 2019-12-12 09:42:10

问题


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?


回答1:


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 =)




回答2:


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.



来源:https://stackoverflow.com/questions/5450387/shell-integrate-in-windows-for-a-specific-file-type-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!