views:

9841

answers:

6

I'd like to to associate a file extension to the current executable in C#. This way when the user clicks on the file afterwards in explorer, it'll run my executable with the given file as the first argument. Ideally it'd also set the icon for the given file extensions to the icon for my executable. Thanks all.

+4  A: 

http://www.codeproject.com/KB/dotnet/System_File_Association.aspx

Then perhaps

http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=32627

Might be of some help. If need be you could always do the registry editing yourself via API directly for the association... but up to you

EDIT: Also read the comments there, some good points eg the use of quotes for paths with spaces: @"C:\SomePath\MyApp.exe ""%1"""

Also permission issues for registry access esp with Vista... good luck :)

White Dragon
A: 

The file associations are defined in the registry under HKEY_CLASSES_ROOT.

There's a VB.NET example here that I'm you can port easily to C#.

Steve Morgan
+4  A: 

There doesn't appear to be a .Net API for directly managing file associations but you can use the Registry classes for reading and writing the keys you need to.

You'll need to create a key under HKEY_CLASSES_ROOT with the name set to your file extension (eg: ".txt"). Set the default value of this key to a unique name for your file type, such as "Acme.TextFile". Then create another key under HKEY_CLASSES_ROOT with the name set to "Acme.TextFile". Add a subkey called "DefaultIcon" and set the default value of the key to the file containing the icon you wish to use for this file type. Add another sibling called "shell". Under the "shell" key, add a key for each action you wish to have available via the Explorer context menu, setting the default value for each key to the path to your executable followed by a space and "%1" to represent the path to the file selected.

For instance, here's a sample registry file to create an association between .txt files and EmEditor:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.txt]
@="emeditor.txt"

[HKEY_CLASSES_ROOT\emeditor.txt]
@="Text Document"

[HKEY_CLASSES_ROOT\emeditor.txt\DefaultIcon]
@="%SystemRoot%\\SysWow64\\imageres.dll,-102"

[HKEY_CLASSES_ROOT\emeditor.txt\shell]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\open]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\open\command]
@="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" \"%1\""

[HKEY_CLASSES_ROOT\emeditor.txt\shell\print]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\print\command]
@="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" /p \"%1\""
X-Cubed
+3  A: 

Also, if you decide to go the registry way, keep in mind that current user associations are under HKEY_CURRENT_USER\Software\Classes. It might be better to add your application there instead of local machine classes.

If your program will be run by limited users, you won't be able to modify CLASSES_ROOT anyway.

Ishmaeel
+1  A: 

There may be specific reasons why you choose not to use an install package for your project but an install package is a great place to easily perform application configuration tasks such registering file extensions, adding desktop shortcuts, etc.

Here's how to create file extension association using the built-in Visual Studio Install tools:

1) Within your existing C# solution, add a new project and select project type as Other Project Types->Setup and Deployment->Setup Project (or try the Setup Wizard)

2) Configure your installer (plenty of existing docs for this if you need help)

3) Right-click the setup project in the Solution explorer, select View->File Types, and then add the extension that you want to register along with the program to run it.

This method has the added benefit of cleaning up after itself if a user runs the uninstall for your application.

Paul J
+1  A: 

If you use ClickOnce deployment, this is all handled for you (at least, in VS2008 SP1); simply:

  • Project Properties
  • Publish
  • Options
  • File Associatons
  • (add whatever you need)

(note that it must be full-trust, target .NET 3.5, and be set for offline usage)

See also MSDN: How to: Create File Associations For a ClickOnce Application

Marc Gravell
I have VS2008 Professional, SP1 and I do not see this option. I must be missing something.
Jerry