2012-01-14 46 views
0

我一直在嘗試使用汽車維護技巧讀取預建文件,在我的「Tips.txt」文件的每行中都有一個。我試圖遵循4或5種不同的方法,但它不工作,編譯但我得到一個異常。下面是我得到了什麼:如何在Windows Phone應用程序中讀取預建文本文件

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (StreamReader sr = new StreamReader(store.OpenFile("Tips.txt", FileMode.Open, FileAccess.Read))) 
      { 
       string line; 
       while ((line = sr.ReadLine()) != null) 
       { 
        (App.Current as App).MyTips.Insert(new DoubleNode(line)); 
       } 
      } 
     } 

我得到這個「不允許操作上IsolatedStorageFileStream」,使用的語句二號裏面的信息。我嘗試將我的「Tips.txt」的構建操作設置爲資源和內容,但我得到了相同的結果。

在此先感謝。

+0

當你說預建,你的意思是你已經它寫入獨立存儲,或者你是否已將其添加到Visual Studio中的項目目錄中? – keyboardP 2012-01-14 20:37:42

+0

將它添加到Visual Studio中的項目目錄 – 2012-01-14 20:38:45

回答

0

由於您已將它添加到項目目錄中,因此無法使用Isolated Storage方法讀取它。有多種方法可以加載文件。一種方法是將文本文件的生成類型設置爲Resource,然後在它讀成流:

//Replace 'MyProject' with the name of your XAP/Project 
Stream txtStream = Application.GetResourceStream(new Uri("/MyProject;component/myTextFile.txt", 
                  UriKind.Relative)).Stream; 

using(StreamReader sr = new StreamReader(txtStream)) 
{ 
    //your code 
} 
+0

我試過了,但現在我得到一個NullReferenceException。 – 2012-01-14 21:07:15

+0

@ JulianJ.Tejera - 你還在聽錯嗎? – keyboardP 2012-01-14 21:15:01

+0

我只是想通了。謝謝:D – 2012-01-14 21:21:46

相關問題