对于我的程序,我必须在程序包中包含大量索引和数据文件。因为它是一个通用应用程序,所以我将这些文件包含在“共享”项目中名为“数据”的文件夹中。
现在我尝试阅读:
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”。怎么了?
如果有人可以帮助我,我得到了文件,我想找到文件大小。我希望,我可以在FileProperties(最后一行代码)中找到此信息。
然后我想在该文件中设置FilePointer并读取定义数量的二进制数据。如果不在内存中读取整个文件,我可以这样做吗?
答案 0 :(得分:4)
您要做的是访问LocalFolder,这与Package.Current.InstalledLocation不同。
如果您想访问包附带的文件,您可以这样做 - 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();
}
或者像这样 - 通过从Package, which you can access as StorageFolder获取文件 - 这里也要注意使用正确的斜杠(因为它可能是异常的来源):
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();
}
另请注意,我已将 Stream 和 BinaryReader 放入使用,因为它们是IDisposable
并且它适合于释放那些不再需要的资源。
另请注意,当您的共享项目名称为 MySharedProject 时,您必须修改上述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);
讨论后的一句话:
将 .txt 扩展名的文件添加到项目时,默认情况下 Build Action 设置为 Content 。但是当你添加带有 .idx 扩展名的文件时,正如我所检查的那样,默认情况下它的 Build Action 设置为 None 。要将这些文件包含在程序包中,请将其更改为 Content 。
答案 1 :(得分:0)
在Romasz带我走上正确的道路之后,我可以看到问题在那里完全不同。 我的数据文件涉及项目中的正确位置,但Visual Studio不会绑定您想要的所有内容。 在我的项目中,我需要将大型数据文件牢固地集成到程序中。它们的大小介于13 KB和41 MB之间,文件类型为.idx和.dat。这些名称是问题的一部分。
到目前为止我所知道的:
我可能会添加看似任意大小的.txt文件。测试41 MB - 没问题。 未添加具有更改的文件类型.idx的相同文件。该文件根本不包含在已编译的项目中。没有错误消息。 当然我可以将.idx文件重命名为另一种文件类型(使用.id测试),但我想知道为什么idx文件的处理方式不同。为什么我没有错误指示。