Handle files from OnFileActivated()

孤者浪人 提交于 2019-12-12 03:53:50

问题


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.



来源:https://stackoverflow.com/questions/35734991/handle-files-from-onfileactivated

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