阅读从Windows Phone 7的文件 [英] Read a file from Windows Phone 7
问题描述
我有一个名为数据/
在我的项目包含 TXT文件
。
I have a folder called data/
in my project that contains txt files
.
我配置生成操作
到资源
来的所有文件。
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());
方法2
method 2
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileNames = myIsolatedStorage.GetFileNames("*.txt");
方法3
method 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
是文件的名称。结果
这是必要的完整路径数据/ 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参考它。相反,使用此功能,一旦发现另外返回空
将返回文件内容。它为我,希望它为你工作。
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.
请注意,如果文件被设置为内容,文件路径= 数据/文件名。 TXT
,但如果它被设置为资源应提到这样的文件路径= /项目名;组件/数据/ FILENAME.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屋!