问题
Possible Duplicate:
Filetype association with application (C#)
I\'m writing a C# Windows app to visualise and modify \'.build\' files (nant scripts). I would like the user to be able to right click on a .build file in windows explorer and select the \'Open With >\' option to allow the file to be modified in my app.
What does my program need to support in-order to work with this mechanism? What might my program need to do to Windows to enable context menu support?
I was wondering if anyone could point me in the direction of a good article/tutorial on this subject.
回答1:
The Open With command just passes the path of the file as the first argument to the application so all you need to do is
public static void Main(string[] args)
{
if(args[0] != null)
{
//args[0] contans a path to the file do whatever you need to do to display it
}
else
{
//Start normally
}
}
To automaticly put your program in the open with list you will need to add some reg keys in HKEY_CLASSES_ROOT\YOUR_EXT\
. Here is a SO answer saying how to do it
Or you could just add it by hand to the open with list the normal way.
回答2:
Take a look at this blog post: Shell Extensions - Context Menu. It has code for a simple "wrapper" to some COM hooks to the Windows shell context menu. Put it in the GAC and when you right-click, your menu will be included as a sub-menu of the right-click context menu.
As far as strictly using "Open With..." to make your application show up ONLY for files it can open, that's a little easier. These are managed by Windows using registry keys in two places in the registry:
- HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ FileExts \ .FileExtension \ OpenWithList (install for current user)
- HKEY_CLASSES_ROOT \ .FileExtension \ OpenWithList (install for all users)
Take a look at some of the existing ones using regedit, then use the Registry class to create a new key for the extension you want.
来源:https://stackoverflow.com/questions/3788429/c-sharp-windows-open-with-context-menu-behaviour