Reputation: 144
I'm really problems when open files stored in my project. I need open some files (pdf, html,...) and ever has the same problem: Value does not fall within the expected range.
I have tried several ways:
a)
private async Task<string> ReadFileContentsAsync(string fileName)
{
StorageFolder foldera = ApplicationData.Current.LocalFolder;
try
{
Stream filea = await foldera.OpenStreamForReadAsync("/Assets/Data/htm/" + fileName + ".htm");
...
}
catch (Exception e)
{
Debug.WriteLine("ERROR ReadFileContentsAsync " + e.Message);
return null;
}
}
b)
private async Task<string> ReadFileContentsAsync(string fileName)
{
try
{
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appdata:///Assets/Data/htm/" + fileName + ".htm", UriKind.RelativeOrAbsolute));
...
}
catch (Exception e)
{
Debug.WriteLine("ERROR ReadFileContentsAsync " + e.Message);
return null;
}
}
c)
StorageFile file2 = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appdata:///Assets/Data/pdf/lc_dossier_acceso_castellana.pdf", UriKind.Absolute));
This actions are launched when I push a button.
I don't know what's happen.
The files are in Solution'NewProject'/NewProject/Assets/Data/*/
Upvotes: 2
Views: 7420
Reputation: 6424
I noticed that I get that error if I use the slash /
in the path to the file. Instead, if I use backslash \
I can get the files.
Try following way:
StorageFile sFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\Data\htm\" + fileName + ".htm");
var fileStream = await sFile.OpenStreamForReadAsync();
Note that you have to place an @
before the path string to avoid the intepretation of \
as scape character.
You could also get the file stream this way:
var fileStream = File.OpenRead("Assets/Data/htm/" + fileName + ".htm");
Upvotes: 12