Zain Rizvi
Zain Rizvi

Reputation: 24636

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

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

Upvotes: 0

Views: 1047

Answers (2)

Zubair Ahmed
Zubair Ahmed

Reputation: 725

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

Upvotes: 1

siger
siger

Reputation: 3162

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.

Upvotes: 3

Related Questions