【问题标题】:How to read a file shipped with the app in Windows 8 Metro如何在 Windows 8 Metro 中读取应用程序附带的文件
【发布时间】:2012-07-29 01:05:00
【问题描述】:

我的应用附带了一个文本文件,我想在屏幕上显示它的内容。有谁知道该怎么做?通常的文件 IO 似乎不适用于 Metro。

谢谢

【问题讨论】:

    标签: file-io windows-8 microsoft-metro


    【解决方案1】:

    不确定您尝试了什么,但请检查一下:

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

    有了 StorageFolder,您就拥有了读取/写入文件的全套功能。

    【讨论】:

    • @mleroy:我在从安装位置访问文件时收到“System.UnauthorizedAccessException”如何解决?
    • @Mahantesh:我认为您应该发布更多关于您如何尝试访问该文件的详细信息。您是否在官方文档中找到了有关您尝试执行的操作的任何参考?
    • @@mleroy:抱歉还有其他问题。?它现在工作正常。
    【解决方案2】:

    在我的应用程序中,我正在读取应用程序附带的 XML 文件,您可以对其进行调整以读取任何类型的文件

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

    在此处查看完整的源代码http://metrorssreader.codeplex.com/SourceControl/changeset/view/18233#263004

    希望有帮助

    【讨论】:

    • 是的,该示例中最重要的部分是 Package.Current.InstalledLocation。这取代了 Silverlight 和 Windows Phone 中的 Assembly.GetManifestResourceStream。
    • @Zubair Ahmed:我在从安装位置访问文件时收到“System.UnauthorizedAccessException”如何解决?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多