Search code examples
windows-phone-8arraysstoragefile

How to write byte array to StorageFile in WindowsPhone 8


I searched and found this but it's not available for Windows Phone 8 and only works for Windows Store apps:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.fileio.writebytesasync

How do I do this in WP8?


Solution

  • You can use binary writer:

    byte[] buffer;   
    
    using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
    {
    
      using (IsolatedStorageFileStream FS = new IsolatedStorageFileStream(fileName, FileMode.Create, ISF))
      {
        using (BinaryWriter BW = new BinaryWriter(FS))
        {
          BW.Write(buffer, 0, buffer.Lenght);
        }
      }
    }
    

    Or make it simpler as @KooKiz said:

    using (IsolatedStorageFileStream FS = new IsolatedStorageFileStream(fileName, FileMode.Create, ISF))
     {
         FS.Write(buffer, 0, buffer.Lenght);    
     }