I have a folder called data/
in my project that contains txt files
.
I configured Build Action
to resources
to all
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";
}