且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

运行时的 Windows Phone 8 资源路径

更新时间:2023-11-19 19:24:34

如果您在项目中包含文件,例如在项目的 Data 文件夹中(在我们的示例中为 json 文件)作为资源,则使用下一个代码获取内容来自该文件:

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;
    }

将 YourProjectName 更改为您的项目名称.在此之后,conten 将 json 文件保存为字符串.

Change YourProjectName to name of you project. After this, conten holds json file as string.

希望对您有帮助