Operation not permitted on IsolatedStorageFileStream. error

前端 未结 4 1583
说谎
说谎 2020-12-06 18:40

I have a problem with isolated storage.

This is my code:

List data = new List();

using (IsolatedStorageFile isoStore = 
           


        
相关标签:
4条回答
  • 2020-12-06 18:51

    Operation not permitted on IsolatedStorageFileStream. error at the time of moving file from shared fileto destination. Its working

    Add Namespaces

     using BackgroundProcess.Resources;
     using Microsoft.Phone.BackgroundTransfer;
     using System.IO.IsolatedStorage;
    

    Create one destination directory in isolated storage

     BackgroundTransferRequest transfer;
     using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    
     {
    
          if (isoStore.GetDirectoryNames("DestinationFolder").Length == 0)
               isoStore.CreateDirectory("DestinationFolder");
    
          storage.MoveFile("/shared/transfers/xyzFileName.mp3", "DestinationFolder");
    
     }
    

    or use

     isoStore.MoveFile(transfer.DownloadLocation.OriginalString, "DestinationFolder");
    

    Instead of adding filename in destination add foldername.

    You can play media by using following code

     try 
     {
          string isoFileName = "DestinationFolder//xyzFileName.mp3";
    
          StorageFile file = null;
    
          try
          {
               file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/" + isoFileName));
          }
          catch (Exception ex)
          {
          }
          if (file != null)
               await Windows.System.Launcher.LaunchFileAsync(file);
      }
      catch (Exception ex)
      {
      }
    
    0 讨论(0)
  • 2020-12-06 18:53

    This usually happens when you execute that code block several times concurrently. You end up locking the file. So, you have to make sure you include FileAccess and FileShare modes in your constructor like this:

    using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.Open, FileAccess.Read, FileShare.Read, isolatedStorage)
    {
    //...
    }
    

    If you want to write to the file while others are reading, then you need to synchronize locking like this:

    private readonly object _readLock = new object();
    
    lock(_readLock)
    {
       using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isolatedStorage)
       {
            //...
       }
    }
    
    0 讨论(0)
  • 2020-12-06 19:09

    Replace the inner using statement with an IsolatedStorageFileStream constructor:

    using ( var isoStream = new IsolatedStorageFileStream( "Notes.xml", FileMode.Open, isoStore ) )
    

    Also, since you're reading from the file, I assume the FileMode you want is Open, not OpenOrCreate.

    And where 'data' is assigned, consider using

    serializer.Deserialize( isoStream ) as List<Notes>
    

    instead. See Item 3 in Effective C#, 2nd Ed.

    0 讨论(0)
  • 2020-12-06 19:14

    In case of Silverlight it can also happen when the full path exceeds a certain character limit. I could not find any official reference for this, but as I have tested in on win10 and IE, it seems to be somewhere between 115 and 120 chars.

    0 讨论(0)
提交回复
热议问题