Reading Files in Windows Phone 8: Value does not fall within the expected range

若如初见. 提交于 2019-12-20 04:16:47

问题


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/*/


回答1:


I noticed that I get that error if I use the shash / 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");


来源:https://stackoverflow.com/questions/17724718/reading-files-in-windows-phone-8-value-does-not-fall-within-the-expected-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!