简体   繁体   中英

C# wpf open serialized file with windows

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.

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

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