thpitsch
thpitsch

Reputation: 2036

How to read data files included in the app

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.

Now I try to read:

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();

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

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).

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?

Upvotes: 2

Views: 8119

Answers (2)

Romasz
Romasz

Reputation: 29792

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

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

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

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.

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

or obtain the suitable StorageFolder:

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

One remark after discussion:

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.

Upvotes: 4

thpitsch
thpitsch

Reputation: 2036

After Romasz has brought me to the right path, I can see the problem is quite different there. My data files were involved in the correct place in the project, but Visual Studio does not bind all what you want. In my project I need large data files to be firmly integrated into the program. These are between 13 KB and 41 MB in size and have file types .idx and .dat. These names are part of the problem.

What I know so far:

I may add .txt files with seemingly arbitrary size. Tested with 41 MB - no problem. The same file with changed file type .idx is not added. The file is simply not included in the compiled project. No error message. Of course I can rename the .idx files to another file type (tested with .id), but I want to know why idx files are treated differently. And why I got no error indication.

Upvotes: 0

Related Questions