如何在Windows Phone应用程序中读取预先构建的文本文件

时间:2012-01-14 20:33:49

标签: file-io windows-phone-7.1

我一直在尝试阅读带有汽车维护技巧的预建文件,我的“Tips.txt”文件的每一行都有一个。我试图遵循4或5种不同的方法,但它不起作用,它编译但我得到一个例外。这就是我所拥有的:

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));
                }
            }
        }

我从第二个using语句中的信息中得到了“IsolatedStorageFileStream上不允许操作”。我尝试将我的“Tips.txt”的构建操作设置为资源和内容,但我得到了相同的结果。

提前致谢。

1 个答案:

答案 0 :(得分: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
}
相关问题