Associate a file extension with WPF application

2019-02-27 09:27发布

问题:

I have a nice little assignment organizer that I want to add a backup option to. But I don't want it in an ordinary xml file or someother file because of the possibility of file corruption. So how can I make a file extension that the program knows and can save to and open with a .asog file extension?

回答1:

Try this:
How does Vista generate the icon for documents associated to my application?

The accepted answer explains icons and file associations.

It doesn't matter that your app uses WPF. The file associations don't care what GUI framework your app uses.



回答2:

If you want to associate a file with extension (.magi) with your WPF application, i advise you to use InnoSetup to do that.

For example, i I developed an Wpf application called MAGI. We assocaite an icon to ".magi" file and when a user click on a ".magi" file it's launch the application and open it directly in the application.


Use InnoSetup to modifiy the registry easyly

Just add this instructino in your iss file :

[Setup]
ChangesAssociations=yes

[Registry]
Root: HKCR; Subkey: ".magi"; ValueType: string; ValueName: ""; ValueData: "MyMAGIApplication"; Flags: uninsdeletevalue
Root: HKCR; Subkey: "MyMAGIApplication"; ValueType: string; ValueName: ""; ValueData: "Program MAGI"; Flags: uninsdeletekey
Root: HKCR; Subkey: "MyMAGIApplication\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\MAGI.EXE,0"
Root: HKCR; Subkey: "MyMAGIApplication\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\MAGI.EXE"" ""%1"""

Parse the arguments in the "Startup" method

We use the Startup property in the main Xaml, in order to call your parser like a usefull main method.

<Application x:Class="MAGI.View.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         Startup="AppStartupMainMagi" >
</Application>

And in the code-behind we add this method

/// <summary>
/// Call with Startup property in App.xaml
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppStartupMainMAGI(object sender, StartupEventArgs e)
{
    String[] arguments = Environment.GetCommandLineArgs();

    if (arguments.GetLength(0) > 1)
    {
        if (arguments[1].EndsWith(".magi"))
        {
            string filePathFormMainArgs = arguments[1];
            if(isFileMagiValid(filePathFormMainArgs)) 
            {
                // Step 1 : deserialize filePathFormMainArgs
                // Step 2 : call the view "File oepn" in the application"
            }
        }
    }
    else {
        // Call the view "welcome page application"
    }
}


回答3:

You can add a file extension with either a Setup project or a ClickOnce install. Once you have it setup, a user can double click on a .asog file and your app will be invoked with the filename as the first entry in the arguments array of main.