Read a file from Windows Phone 7

前端 未结 1 596
庸人自扰
庸人自扰 2021-01-13 18:43

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

I configured Build Action to resources to all

相关标签:
1条回答
  • 2021-01-13 19:41

    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.

    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";
        }
    
    0 讨论(0)
提交回复
热议问题