简体   繁体   中英

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

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.

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

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

See this . Or this if you want API information.

ClickOnce supports file associations as of .NET 3.5 SP1, too. In the project's properties, switch to the Publish tab and click the Options button. There's a File Associations section in that dialog that allows you to specify file extensions, descriptions and custom icons.

First, you have to associate the filetype extention with your executeable. On Windows you do this via the registry (search "filetype association windows"). In this question you find some interesting hints: Filetype association with application (C#) Script to associate an extension to a program

Your program has to react on the command line arguments or parameters. In Java, it is indeed the string array of the main method. I would gess, it's the same in C#.

如果您不需要以编程方式执行此操作,请右键单击图标,单击打开...,然后选择“始终使用此程序...”。

This is something usually handled by your setup program .. I've used INNO setup for example, and it's trivially simple to arrange for it to adjust user's registry to launch your app when associated file extension is double clicked/opened. It'll even take care of MIME types for you as well as clearing these things on uninstall, which is a very nice thing to do

I managed to solve this issue. I used WIX to create an install file and asked it to associate the file with the application when it installs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM