Piotr Nowak
Piotr Nowak

Reputation: 85

Why does Application.GetResourceStream always return null in WP7?

I am creating a Windows Phone app and I will be storing level data in the text files.

I have the following problem: I am trying to read all the lines from the file and every time I call Application.GetResourceStream it returns null.

I know this topic was asked by different people, but all the solutions didn't work for me.

This is the code:

List<String> fileLines = new List<string>();
var resource = Application.GetResourceStream(
                         new Uri("Level_data/Level_1.txt", UriKind.Relative));
StreamReader streamReader = new StreamReader(resource.Stream);
string line;
while ((line = streamReader.ReadLine()) != null)
    fileLines.Add(line);

Also, the solution explorer looks like this. The Build Action is set to "Resource", but I also tried "Content" first and didn't work either.

Upvotes: 0

Views: 5163

Answers (1)

Piotr Nowak
Piotr Nowak

Reputation: 85

The following code worked:

    Uri u = new Uri("WindowsPhoneGame1;component/Level_data/Level_1.txt",
                                                             UriKind.Relative);
    StreamResourceInfo sInfo=Application.GetResourceStream(u);
    StreamReader sr=  new StreamReader(sInfo.Stream);
    string line;
    while ((line = sr.ReadLine()) != null)
         fileLines.Add(line);
    sr.Close();

The only reason why it wasn;t working earlier was that the Path for the Uri was wrong. Now it is: WindowsPhoneGame1;component/Level_data/Level_1.txt i.e. (ProjectName);component/(RelativePath)

Upvotes: 7

Related Questions