c# - 在Windows Phone 8中读取文件:值不在预期范围内

标签 c# windows-phone-8 readfile

打开项目中存储的文件时,我确实遇到了问题。我需要打开一些文件(pdf,html,...),并且遇到相同的问题:值不在预期范围内。

我尝试了几种方法:

一种)

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


当我按下按钮时,将启动此操作。

我不知道怎么回事

这些文件位于Solution'NewProject'/ NewProject / Assets / Data / * /中

最佳答案

我注意到如果在文件路径中使用shash /,则会收到该错误。相反,如果我使用反斜杠\,则可以获取文件。

请尝试以下方式:

StorageFile sFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\Data\htm\" + fileName + ".htm");

var fileStream = await sFile.OpenStreamForReadAsync();


请注意,必须在路径字符串之前放置@,以避免将\解释为换码符。

您还可以通过以下方式获取文件流:

var fileStream = File.OpenRead("Assets/Data/htm/" + fileName + ".htm");

关于c# - 在Windows Phone 8中读取文件:值不在预期范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17724718/

相关文章:

mvvm - 有谁知道 ReactiveUI 的 ReactiveCommand 的好例子吗?

c# - 如何消除 Switch 语句?

c# - 如何在一行中将字符串拆分和修剪成多个部分?

c# - 如何阻止 IIS 上的 WCF 服务变为空闲

c# - INotifyPropertyChanged 的​​ PropertyChanged 成员始终为 null

c# - "An error occurred creating the configuration section handler for"-

windows-phone-8 - Windows Phone 8 的内存使用限制

node.js - 对事件循环、多线程、Node.js 中 readFile() 的执行顺序有疑问?

Java读取文本文件以获取特定格式的值

python - 如何读取文本文件并转换为字典?