Windows Phone 8 - reading and writing in an existing txt file in the project

会有一股神秘感。 提交于 2019-12-01 12:31:18

I created a helper function in my WP 7 project recently, to read a text file included in the project. You can try to use it, the function also working in WP 8 project :

public static class FileHelper
{
    public static string ReadFile(string filePath)
    {
        var ResrouceStream = Application.GetResourceStream(new Uri(filePath, UriKind.Relative));
        if (ResrouceStream != null)
        {
            Stream myFileStream = ResrouceStream.Stream;
            if (myFileStream.CanRead)
            {
                StreamReader myStreamReader = new StreamReader(myFileStream);

                return myStreamReader.ReadToEnd();
            }
        }
        return "";
    }
}

Then I can use that function this way (in this example the file resides under Assets folder) :

var textFileContent = FileHelper.ReadFile(@"Assets\MyTextFile.txt");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!