2015-04-21 1 views
0

Je fais un Windows Phone 8.1 Silverlight, et lorsque je tente de lire un fichier, et à la lecture, l'erreur suivante:System.UnauthorizedAccessException sur File.OpenRead

Une exception de type « système .UnauthorizedAccessException » a eu lieu dans mscorlib.ni.dll mais n'a pas été traitée dans le code utilisateur

informations complémentaires: l'accès au chemin 'C: \ Users Data \ \ public \ Pictures \ Pellicule \ WP_20150421_001.jpg' est refusé.

{ 
    string filepath = FilePathTextBox.Text; 
    string cryptPath = filepath; 
    File.SetAttributes(cryptPath, System.IO.FileAttributes.Normal); 

    FileStream stream = File.OpenRead(cryptPath); 
    byte[] fileBytes = new byte[stream.Length]; 

    stream.Read(fileBytes, 0, fileBytes.Length); 
    stream.Close(); 

    byte[] ProtectedFileBytes = ProtectedData.Protect(fileBytes, null); 
    using (Stream file = File.OpenWrite(@"C:\Data\Users\Public\Documents")) 
    { 
     file.Write(ProtectedFileBytes, 0, ProtectedFileBytes.Length); 
    } 
    MessageBox.Show("File Sucessfully Encrypted"); 
} 

Toutes les bibliothèques pertinentes sont vérifiées dans le manifeste, je suis en cours d'exécution Visual Studio en tant qu'administrateur, j'ai essayé différents fichiers, en vain.

J'ai ajouté une vérification pour vérifier si le fichier existe, et marqué comme faux, mais cela est probablement dû au problème d'autorisations.

Des suggestions pour résoudre ce problème?


Edit: J'ai maintenant ce code, qui jette un System.ArgumentException:

private async void EncryptButton_Click(object sender, RoutedEventArgs e) 
    { 
     string filepath = FilePathTextBox.Text; 
     string cryptPath = filepath; 

     await FileOps(); 
      FileStream stream = File.OpenRead(cryptPath); 
      byte[] fileBytes = new byte[stream.Length]; 

      stream.Read(fileBytes, 0, fileBytes.Length); 
      stream.Close(); 

      byte[] ProtectedFileBytes = ProtectedData.Protect(fileBytes, null); 
      using (Stream file = File.OpenWrite(@"C:\Data\Users\Public\Documents")) 
      { 
       file.Write(ProtectedFileBytes, 0, ProtectedFileBytes.Length); 
      } 
      MessageBox.Show("File Sucessfully Encrypted"); 
    } 

    private async Task FileOps() 
    { 
     string filepath = FilePathTextBox.Text; 
     string cryptPath = filepath; 
     StorageFolder storageFolder = KnownFolders.CameraRoll; 
     StorageFile file = await storageFolder.CreateFileAsync(cryptPath, CreationCollisionOption.ReplaceExisting); 
    } 

Répondre

1

Je pense que le problème est que vous ne pouvez pas simplement l'accès par le chemin ... Vous aurez besoin de passer par les connus dossiers:

KnownFolders class

Quickstart: Working with files and folders in Windows Phone 8

// Get the folder. 
StorageFolder camera = Windows.Storage.KnownFolders.CameraRoll; 

if (local != null) 
{ 

    // Get the file. 
    var file = await camera.OpenStreamForReadAsync("WP_20150421_001.jpg"); 

    // Read the data. 
    using (StreamReader streamReader = new StreamReader(file)) 
    { 
     //streamReader.ReadToEnd(); 
    } 

} 
+0

J'ai essayé d'accéder au dossier Documents, et le dossier Photos pour obtenir des fichiers à partir de là, avec le même résultat, cela s'appliquerait-il également? – user3279195

+0

Vous devez également mettre à jour votre fichier manifeste d'assembly pour demander l'autorisation –

+0

@ user3279195 Oui, vous utiliseriez 'KnownFolders.DocumentsLibrary' – bdimag