Associate a file extension with WPF application

╄→гoц情女王★ 提交于 2019-12-01 22:12:22
Cheeso

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.

v20100v

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.


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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!