0

I've a custom file. Serialization and deserialization is working fine when using my custom functions

  • File > Save
  • File > Open

When I'm going directly in the windows explorer on my file and want to open it with the program it's not deserializing. How can I handle the deserialization from "outside"?

Thanks for your help.

  • 2
    When you do "open with..." in windows, the path of the file will be an argument to your `Main` function. You can get some ideas here, I guess: https://stackoverflow.com/questions/6088961/handle-program-being-opened-by-open-with (also check the linked duplicate) . It's up to you to properly read the argument given in main, and act accordingly in your program (for instance, directly call the deserialization function you have) – Pac0 Jul 29 '20 at 21:33

1 Answers1

0

When a file is opened from Windows Explorer with your app, the absolute path of the file is passed as the first command line argument. In case of WPF, you can handle the Startup event of App to intercept such an argument for later opening in MainWindow.

// App.xaml.cs
public string FileToOpen;

public App()
{
    Startup += (sender, e) =>
    {
        if (e.Args.Length > 0)
            FileToOpen = e.Args[0];
    };
}
// MainWindow.xaml.cs
public MainWindow()
{
    var path = (Application.Current as App).FileToOpen;
    if (path != null)
    {
        // TODO: open the file when appropriate
    }
}
TimTIM Wong
  • 788
  • 5
  • 16
  • Thanks, that worked fine. Do you know how it is possible how to find out whether the app is already opened? Because when it is and I open a file with the explorer, I'll get a new instance of my app. That's a bit annoying. – user23623447 Aug 03 '20 at 18:42
  • Yes, but that's another question. – TimTIM Wong Aug 03 '20 at 18:43