How to read a file shipped with the app in Windows 8 Metro

我的未来我决定 提交于 2019-12-12 04:57:59

问题


I've got a text file that ships with my app and I want to display its contents on the screen. Anyone know how to do that? The usual file IO doesn't seem to work for Metro.

Thanks


回答1:


In my app I am reading a XML file that comes with the app, you can tweak it to read any type of file

public class LocalStorage
{
    private const string SyndicationFeedCategoriesFileName = "FeedCategories.xml";

    private StorageFile _storageFile;
    private StorageFolder _storageFolder;
    public async Task<XmlDocument> Read_categories_from_disk()
    {

        try
        {
            _storageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Xml");
            _storageFile = await _storageFolder.GetFileAsync(SyndicationFeedCategoriesFileName);

            var loadSettings = new XmlLoadSettings
                                   {ProhibitDtd = false, ResolveExternals = false};
            return await XmlDocument.LoadFromFileAsync(_storageFile, loadSettings);
        }
        catch (Exception)
        {
            return null;
        }
    }
}

View the full source code here http://metrorssreader.codeplex.com/SourceControl/changeset/view/18233#263004

Hope that helps




回答2:


Not sure what you've tried, but check this out:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.package.installedlocation.aspx

With the StorageFolder in hand, you have the whole set of functions to read/write files.



来源:https://stackoverflow.com/questions/11705866/how-to-read-a-file-shipped-with-the-app-in-windows-8-metro

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