Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an application open when a user double clicks on its associated file?

Tags:

c#

I am creating an application that uses a certain file format as its data source. I want this application to open whenever the user double clicks on this file, like how MS Word will open when a user double clicks on a Word document. How do I accomplish this? Also how would I populate the data fields using the file that the user selected. Would I use args[] from the program.cs class? I am using c# to code this application.

N.B. I want this association to be made when the application is installed on the host machine without the user doing anything.

like image 982
Draco Avatar asked Mar 03 '09 07:03

Draco


People also ask

When you double click and the associated file or program gets open?

Answer: On icons. ... Double-clicking an application program will launch the program, and double-clicking a file icon will open the file in a default application for that file's type or format.

What happens when you double click a file on the desktop?

In most cases, a double-click is with the left mouse button and is used to open or execute a file, folder, or software program. For example, programs and files in Microsoft Windows are opened by double-clicking the icon. To open your Internet browser, you would double-click the browser icon shortcut.


1 Answers

FIRST, you need to set up file association, so that your file type is associated with your application and opening the file type will run your application.

You can do the file association programatically, there is some detail here as mentioned: http://www.codeproject.com/KB/dotnet/System_File_Association.aspx

You can also do it via your Setup project for you application if you have one. This is an easier path for "newbies". Details for using visual studio to get the setup project to add the file association and also set the icon for the file are here: http://www.dreamincode.net/forums/topic/58005-file-associations-in-visual-studio/

Otherwise if you use InnoSetup, Wix etc then I suppose you could just see instructions for those installers to create the association for you.

SECOND, you need to have your application accept command line arguments. The opened file(s) is(are) passed as a command line argument(s). You need to process the arguments to get the file path/name(s) and open the given file(s). There is a nice description of this here with code: C# Command Line arguments problem in Release build

In your case, rather than MessageBox.Show(s) in the form shown handler, you would call your bespoke argument parsing method. For a simple application which only accepts files names to open as arguments, this could be as simple as

foreach (string filePathName in Args)
                DoNamedFileOpen(filePathName);

Your code can also have a method that might extract from the file the values for the datafields you are interested in etc.

This is a nice simple approach to the issue of have file associations set on installation of your application, with icons, and having your application handle the opening of those files.

Of course, there are plenty of other options, like run-time file association (asking the user if they want the association), detecting "broken" associations, etc.

This question is a long time here but I hope this is useful for new searches

like image 112
abe Avatar answered Sep 27 '22 23:09

abe