3

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?

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
  • Does this answer your question? [How does Vista generate the icon for documents associated to my application?](https://stackoverflow.com/questions/580827/how-does-vista-generate-the-icon-for-documents-associated-to-my-application) – StayOnTarget Sep 11 '20 at 12:48

3 Answers3

3

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.

Community
  • 1
  • 1
Cheeso
  • 189,189
  • 101
  • 473
  • 713
3

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.

Open file extension in Wpf 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"
    }
}
Elliott Beach
  • 10,459
  • 9
  • 28
  • 41
v20100v
  • 696
  • 4
  • 11
  • tried, it's working, but application crashed, because content folder access denied !. How to get items like icons, images from content folder? – Jinesh Jul 16 '18 at 07:53
1

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.

Jake Pearson
  • 27,069
  • 12
  • 75
  • 95