从 Windows Phone 7 读取文件 [英] Read a file from Windows Phone 7

查看:20
本文介绍了从 Windows Phone 7 读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个名为 data/ 的文件夹,其中包含 txt 文件.

I have a folder called data/ in my project that contains txt files.

我将 Build Action 配置为 resources 到所有文件.

I configured Build Action to resources to all files.

我尝试了这些不同的方法:

I tried these different ways:

var resource = Application.GetResourceStream(new Uri(fName, UriKind.Relative));
StreamReader streamReader = new StreamReader(resource.Stream);
Debug.WriteLine(streamReader.ReadToEnd());

方法二

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileNames = myIsolatedStorage.GetFileNames("*.txt");

方法 3

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
   using (StreamReader fileReader = new StreamReader(new IsolatedStorageFileStream(fName, FileMode.Open, isf)))
   {
      while (!fileReader.EndOfStream)
      {
        string line = fileReader.ReadLine();
        al.Add(line);
        Debug.WriteLine(line);
      }
   }
}

现在,我尝试了不同的方法来读取文件没有成功,为什么?
问题出在哪里?

Now, i tried different ways to read files without success, why?
Where is the problem?

这些方法有什么问题?

fName 是文件的名称.
是否需要完整路径 data/filename.txt?无所谓...

fName is the name of the file.
It's necessary the full path data/filename.txt? It's indifferent...

请帮我解决这个愚蠢的问题,
谢谢.

please help me with this stupid issue,
thanks.

推荐答案

您的第二个 &第三种方法是错误的.当您在应用程序中本地包含文本文件时,您无法通过 IS 引用它.相反,使用此函数,如果找到,它将返回文件内容,否则将返回 "null".它对我有用,希望对你有用.

Your 2nd & 3rd approaches are wrong. When you include a text file locally in your app, you can't refer it via the IS. Instead, use this function, it will return the file content if found else it will return "null". It works for me, hope it works for you.

注意,如果文件被设置为内容,filePath = "data/filename.txt" 但如果它被设置为资源,它应该像这样 filePath = "/项目名称;组件/数据/文件名.txt".这可能就是您的第一种方法可能失败的原因.

Note, if the file is set as content, the filePath = "data/filename.txt" but if it is set as resource it should be referred like this filePath = "/ProjectName;component/data/filename.txt". That may be why your 1st approach might have failed.

    private string ReadFile(string filePath)
    {
        //this verse is loaded for the first time so fill it from the text file
        var ResrouceStream = Application.GetResourceStream(new Uri(filePath, UriKind.Relative));
        if (ResrouceStream != null)
        {
            Stream myFileStream = ResrouceStream.Stream;
            if (myFileStream.CanRead)
            {
                StreamReader myStreamReader = new StreamReader(myFileStream);

                //read the content here
                return myStreamReader.ReadToEnd();
            }
        }
        return "NULL";
    }

这篇关于从 Windows Phone 7 读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆