windows phone 8 resource path at runtime

女生的网名这么多〃 提交于 2019-12-25 09:31:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!