2011-11-25 61 views
0

我想讀使用BinaryReader在一個二進制文件的二進制文件,但我不斷收到一個例外:讀數窗電話

using (var stream = File.Open("file.bin", FileMode.Open, FileAccess.Read)) 
     { 
      using (BinaryReader r = new BinaryReader(stream)) //EXCEPTION 
      { 

      } 
     } 

的「file.bin」已被設置爲在構建行動的內容,但我不斷收到此異常:

System.MethodAccessException了未處理

嘗試訪問該方法失敗:System.IO.File.Open(System.String,System.IO.FileMode,System.IO.FileAccess )

回答

1

在Windows Phone 7上不使用File.Open - 必須使用isolated storage

有關更多詳細信息,請參閱System.IO.IsolatedStorage命名空間。

例如:

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (var stream = store.OpenFile("file.bin", FileMode.Open)) 
    { 
     using (var reader = new BinaryReader(stream)) 
     { 

     } 
    } 
} 

編輯:正如在評論中指出,對於內置在XAP內容,你應該使用Application.GetResourceStream

+0

但我需要從我的xap文件的內容中讀取文件。我可以使用IsolatedStroage訪問這些文件嗎? – Ateik

+0

@ user836252:啊,你應該說。不,這些不會被隔離存儲。我相信你需要['Application.GetResourceStream'](http://msdn.microsoft.com/en-us/library/ms596994(v = VS.95).aspx)。看到[這個類似的問題](http://stackoverflow.com/questions/5045456/how-to-read-text-from-a-text-file-in-the-xap)。 –

+0

我已經說過它已經被設置爲構建動作中的內容lol,但是,謝謝,它使用GetResourceStrea – Ateik