Handle files from OnFileActivated()

2019-09-03 12:02发布

问题:

I have problem with handling files passed to my application in OnFileActivated(). First, I've registred specific file extention in Package.appminifest of my application, so after tap into specific file my application starts and run OnFileActivated function.

In my case file is archive zipped with System.IO.Compression.ZipArchive, but I think it's not crutial here. Beggining of my function looks as follow:

    protected override async void OnFileActivated(FileActivatedEventArgs args) {
        base.OnFileActivated(args);

        var file = args.Files[0];
        using (var archive = ZipFile.OpenRead(file.Path)) {
        ...

As some of you can expect I get an error when I'm trying to access file in last line. I also tried different solutions as copying file into local folder and then access it, but also without luck.

Question Is there any way to do such thing? Or maybe I'm doing it completely wrong way?

回答1:

Using the Path property will not be useful for a brokered file (such as you get from Activation). Use the constructor that takes a Stream instead.



回答2:

Here is the correct answer:

    protected override async void OnFileActivated(FileActivatedEventArgs args) {
        base.OnFileActivated(args);

        var file = (StorageFile)args.Files[0];

        using (var archive = new ZipArchive(await file.OpenStreamForReadAsync())) {
        ...

I haven't noticed before that ZipArchive have constructor which takes stream as a parameter.