我似乎无法从 Windows 商店应用程序的后台任务中读取文件。这是读取文件内容的代码:
async private static Task<string> ReadAsync(string FileName)
{
var folder = ApplicationData.Current.LocalFolder;
var file = await folder.GetFileAsync(FileName);
Windows.Storage.Streams.IRandomAccessStreamWithContentType inputStream = null;
try
{
inputStream = await file.OpenReadAsync();
}
catch (Exception ex)
{
throw (ex);
}
string content = string.Empty;
using (Stream stream = inputStream.AsStreamForRead())
{
using (StreamReader reader = new StreamReader(stream))
{
try
{
// *** program exits on this line
content = await Task.Run(() => reader.ReadToEnd());
}
catch(Exception ex)
{
// no error is caught
content = ex.Message;
}
}
}
return content;
}
程序在 StreamReader 上调用 ReadToEnd() 的行上退出- 在 try catch 块中没有捕获到错误。在输出窗口中,我得到:
程序“[8968] backgroundTaskHost.exe: Managed (v4.0.30319)”已退出,代码为 1 (0x1)
是否可以通过后台任务访问文件?如果是这样,我哪里错了?