0

I'm trying to access a .js file. The file is located inside Database folder and named myfile.js which is just a JSON formatted text-file.

This is the code I'm using (this is run from MainPage.xaml.cs:

Uri uri = new Uri("/Database/myfile.js", UriKind.Relative);
var resource = Application.GetResourceStream(uri);

StreamReader streamReader = new StreamReader(resource.Stream);
string rawData = streamReader.ReadToEnd();

It always throw an exception of NullReferenceException because the resource variable is null.

So, the problem is in the Uri. I tried to use:

@"/Database/myfile.js"
"/Database/myfile.js"
"/LQI;component/Database/myfile.js"

But it doesn't work. When I tried to move my file to root directory and replace the Uri string with

@"myfile.js"

it works! But I don't want that.

I also tried to rename the extension become .txt but nothing.

Please help. Thanks!

Edit: I set the Build Action to "Content" which is default. I tried to set it to "Resource" but it doesn't work neither.

Edit #2: Okay, I found a clue (https://stackoverflow.com/a/12122332/2649132) that the LQI;component/Database/myfile.js only work when I set the item into Resource. And it does work. But, the question will be still open since I doesn't work when I set the build action to "Content" with regular path.

4

1 回答 1

2

该路径是相对路径,因此请从路径字符串中取出前导“/”,使其显示为“Database/myfile.js”,它应该可以正常工作。

于 2013-09-09T18:55:12.143 回答