简体   繁体   中英

How to check if file inside XAP exists?

I'm writing a Windows Phone 7 Application that loads sample data from XML files deployed inside the XAP. These XML files are localized, so there is on file for each culture supported.

Structure:

SampleData.xml
SampleData.de.xml
SampleData.fr.xml
SampleData.it.xml
...

The name of the XML to load is based on the current UI culture. It's easy to build the name of the file, but how do I check if a localized version of the current UI culture exists?

System.IO.File.Exists() is not available in Windows Phone 7. I could create a dummy file for each culture that is supported by the phone, but that's quite ugly, error prone and does not allow a fallback to the neutral language. Any ideas?

private bool IsFileExists(string relativePath)
{
    return Application.GetResourceStream(new Uri(relativePath, UriKind.Relative)) != null;
}

relativePath should NOT begin with a slash

AFAIK there is no way to enumerate the files contained within a XAP file. Since you're the one creating the XAP you should know what files exist and which ones don't. If you're trying to load the localized version, or fall back to a default version if the former doesn't exist, then, instead of enumerating files in the package, you could create another file that lists all the localized versions available.

Alternately, you could use something like XDocument.Load() within a try-catch. It'll probably throw a FileNotFoundException which you can handle and then load the default version.

如果文件标记为构建操作“资源”,则必须在本地路径前添加“/; component /”字符串以成功加载资源。

return Application.GetResourceStream(new Uri("/PhoneProject1;component/" + relativePath, UriKind.Relative)) != null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM