2012-01-14 58 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

相关问题