简体   繁体   中英

Associate a file extension with WPF application

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?

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.

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 developed an WPF application called MAGI. We associate an icon to " .magi" file and when a user clicks on a " .magi" file it launches the application and opens it directly in the application.

在WPF应用程序中打开文件扩展名


Use InnoSetup to modify the registry easily

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 useful 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"
    }
}

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.

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