0

Я пытался копировать файлы PDF, когда пользователь выбирает файл из любого места, связывая тип файла с моим приложением.Получение несанкционированного исключения при чтении файла по методу onfile

, к сожалению, я получить несанкционированный исключение при попытке скопировать файл

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(); 

    } 
+0

Предполагая, что ваш 'tm' является StorageFile, вы пытались сделать' tm.CopyAsync (журнал), '? – Romasz

+0

Да, но та же ошибка –

+0

И когда вы не перенаправляете работу на ThreadPool - просто запустите 'await (e.Files.FirstOrDefault() как StorageFile) .CopyAsync (magazineFolder);' Вы также получаете исключение? Вы отлаживали и проверяли, есть ли у вас действительный StorageFile? Возможно ли, что вы можете поделиться образцом проекта? – Romasz

ответ

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(); 
    }