如何读取应用程序中包含的数据文件 [英] How to read data files included in the app

查看:78
本文介绍了如何读取应用程序中包含的数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的程序,我必须在程序包中包含巨大的索引和数据文件.因为它是一个通用应用程序,所以我将这些文件包含在共享"项目中名为Data"的文件夹中.

For my program I have to include huge index and data files in the program bundle. Because it is an universal app, I have included these files in a folder named "Data" within the "Shared" Project.

现在我尝试阅读:

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("Data/"+fileName);
Stream stream = (await file.OpenReadAsync()).AsStreamForRead();
BinaryReader reader = new BinaryReader(stream);
Windows.Storage.FileProperties.BasicProperties x = await file.GetBasicPropertiesAsync();

我在第一行收到 System.ArgumentException "mscorlib.ni.dll".怎么了?

I get a System.ArgumentException "mscorlib.ni.dll" at the first line. What's wrong?

如果有人可以帮助我并且我得到了文件,我想找到文件大小.我希望,我可以在 FileProperties(最后一行代码)中找到这些信息.

If somebody can help me and I get the file, I want to find the filesize. I hope, I can find this Information within the FileProperties (last line of code).

然后我想在该文件中设置一个 FilePointer 并读取定义数量的二进制数据.我可以在不读取内存中的整个文件的情况下做到这一点吗?

Then I want to set a FilePointer within that file and to read a defined number of binary data. Can I do that without reading the whole file in memory?

推荐答案

您尝试做的是访问 LocalFolder,与 Package.Current.InstalledLocation.

What you are trying to do is to access LocalFolder, which is not the same as Package.Current.InstalledLocation.

如果您想访问包含在您的中的文件,您可以这样做 - 通过使用 URI 方案:

If you want to access files that are included with your package, you can do for example like this - by using URI schemes:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Data/"+fileName));
using (Stream stream = (await file.OpenReadAsync()).AsStreamForRead())
using (BinaryReader reader = new BinaryReader(stream))
{
   Windows.Storage.FileProperties.BasicProperties x = await file.GetBasicPropertiesAsync();
}

或者像这样 - 通过从您的 包,您可以作为 StorageFolder 访问 - 在这里也要注意使用正确的斜杠(因为它可能是异常的来源):

or like this - by getting file from your Package, which you can access as StorageFolder - also pay attention here to use correct slashes (as it may be a source of exception):

StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Data\" + fileName);
using (Stream stream = (await file.OpenReadAsync()).AsStreamForRead())
using (BinaryReader reader = new BinaryReader(stream))
{
   Windows.Storage.FileProperties.BasicProperties x = await file.GetBasicPropertiesAsync();
}

另请注意,我已将您的 StreamBinaryReader 放入 using 中,因为它们是 IDisposable 和那些不再需要的资源适合释放.

Note also that I've put your Stream and BinaryReader into using, as they are IDisposable and it's suitable to release those resources as they are no longer needed.

另请注意,当您的共享项目具有名称 MySharedProject 时,您必须修改上述 URI 的路径:

Note also that when your shared project has a name MySharedProject, you will have to modify the Path of above URI:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///MySharedProject/Data/"+fileName));

或获取合适的StorageFolder:

StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"MySharedProject\Data\" + fileName);

<小时>

讨论后的一句话:


One remark after discussion:

当您向项目中添加带有 .txt 扩展名的文件时,其构建操作默认设置为 Content.但是当你添加带有 .idx 扩展名的文件时,正如我所检查的,它的 Build Action 默认设置为 None.要将这些文件包含在您的包中,请将它们更改为 Content.

When you add a file with .txt extension to your project, its Build Action by default is set to Content. But when you add file with .idx extension, as I've checked, its Build Action is set to None by default. To include those files in your package, change them to Content.

这篇关于如何读取应用程序中包含的数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆