在onfileactivated方法上读取文件时出现未经授权的异常

问题描述 投票:0回答:1

当用户通过将文件类型与我的应用程序相关联从任何位置选择文件时,我试图复制PDF文件。

不幸的是,我在尝试复制文件时遇到了未经授权的异常

  async  protected override void OnFileActivated(FileActivatedEventArgs e)
    {//IReadOnlyList<IStorageItem>


        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {

            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            if (!rootFrame.Navigate(typeof(MainPage)))
            {
                throw new Exception("Failed to create initial page");
            }
        }

        StorageFolder magazinefolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("pdffolder", CreationCollisionOption.OpenIfExists);
        IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
(workItem) =>
{




    foreach (IStorageItem tm in e.Files)
    {

        File.Copy(tm.Path, magazinefolder.Path);

    }


    //   this.loadlibrary();






});

        var p = rootFrame.Content as MainPage;
        // p.AddFileCall(e.Files);

        // Ensure the current window is active
        Window.Current.Activate();

    }
c# xaml windows-store-apps win-universal-app windows-10-universal
1个回答
0
投票

而不是将文件作为IstorageItem,我们可以直接将其作为StorageFile来解决问题

 async public void AddFileCall(Windows.ApplicationModel.Activation.FileActivatedEventArgs e)
    {
        StorageFolder magazinefolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(Constants.MagFolder, CreationCollisionOption.OpenIfExists);
        foreach (StorageFile file in e.Files)
        {
            await file.CopyAsync(magazinefolder,file.Name,NameCollisionOption.GenerateUniqueName);
        }

        MessageDialog Sucess = new MessageDialog("File Imported", "Thank You");
        await Sucess.ShowAsync();
        this.loadlibrary();
    }
© www.soinside.com 2019 - 2024. All rights reserved.