问题
I have to include some files with my windows phone 8 app. the app will accordingly get those file from resource and read or show partly the content according to the algorithm. how do i get the path to my embedded resource?
回答1:
If you include file in you project, for instance in Data folder of your project (in our case json file) as a resource then use next code to get content from that file:
string content = string.Empty;
string resource_file = "Data/myfile.json";
if (IsLocalResourceFileExists(resource_file))
{
var resource = Application.GetResourceStream(new Uri(@"/YourProjectName;component/" + resource_file, UriKind.Relative));
StreamReader streamReader = new StreamReader(resource.Stream, System.Text.Encoding.UTF8);
content = streamReader.ReadToEnd();
streamReader.Close();
}
To check if file exist in resource use this:
public bool IsLocalResourceFileExists(string relativePath)
{
return Application.GetResourceStream(new Uri(@"/YourProjectName;component/" + relativePath, UriKind.Relative)) != null;
}
Change YourProjectName to name of you project. After this, conten holds json file as string.
Hope this help
来源:https://stackoverflow.com/questions/21098324/windows-phone-8-resource-path-at-runtime