从IsolatedStorage读取文件作为字节

时间:2012-09-12 11:02:07

标签: isolatedstorage windows-phone-7.1.1

我希望将隔离存储中的文件大小作为字节。还需要将该文件读取为每个1024字节的块。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

public static IsolatedStorageFileStream OpenFile(string filePath)
    {
        IsolatedStorageFileStream fileStream = null;

        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!store.FileExists(filePath))
            {
                return fileStream;
            }
            else
            {
                fileStream = store.OpenFile(filePath, FileMode.Open,     FileAccess.ReadWrite, FileShare.ReadWrite);
        }
        return fileStream;
    }

void ReadFile()
 {
    using (var streamReader = OpenFile(filePath))
    {
      using (BinaryReader reader = new BinaryReader(streamReader))
      {
         byte[] buffer = new byte[1024];
         int fileLen = (int)reader.BaseStream.Length;
         int readCount=0;
         while (fileLen > 0)
         {
            readCount = reader.Read(buffer, 0, buffer.Length);
            fileLen = fileLen - readCount;
            //Do what ever with buffer
         }
       }}}