C# GetFileAsync在保存文件后不返回该文件

C# GetFileAsync在保存文件后不返回该文件,c#,windows-phone-8.1,windows-8.1,storagefile,C#,Windows Phone 8.1,Windows 8.1,Storagefile,我有以下方法,将文件保存到Windows Phone上的本地文件夹 public async Task<bool> SaveValueByKey(string filename, KeyValuePair<string, string> dataToSave) { var appData = ApplicationData.Current; // Good Save documentation - http://chriskoenig.net/2012/

我有以下方法,将文件保存到Windows Phone上的本地文件夹

public async Task<bool> SaveValueByKey(string filename, KeyValuePair<string, string> dataToSave)
{
    var appData = ApplicationData.Current;

    // Good Save documentation - http://chriskoenig.net/2012/09/07/windows-8-games-and-roaming-data/
    StorageFile file = await appData.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);

    try
    {
        // Delete the key if it already exists, then replace.
        IEnumerable<string> content = await this.LoadFileContents(filename);
        if (content.Any(l => l.StartsWith(string.Format("{0}==", dataToSave.Key))))
        {
            await this.DeleteKey(filename, dataToSave.Key);
        }

        await FileIO.WriteTextAsync(file, string.Format("{0}=={1}", dataToSave.Key, dataToSave.Value));
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}
编辑2


引发的异常是FileNotFound异常,并显示以下消息

The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Lifestream.App.Mobile.WindowsApp.Services.FileStorageService.<LoadValueFromKey>d__16.MoveNext()
系统找不到指定的文件。(HRESULT的异常:0x80070002)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在Lifestream.App.Mobile.WindowsApp.Services.FileStorageService.d_u16.MoveNext()中
编辑3


如果我以Windows应用程序(这是一个通用应用程序)的形式运行该应用程序,则保存和加载文件时不会出现任何问题。如果我以Windows Phone为目标,则在尝试保存然后加载时会引发“未找到文件”异常。这似乎是针对手机的,我不明白为什么我能解决这个问题。问题在于Windows Phone项目属性。属性->调试选中了“卸载然后重新安装我的软件包”选项,该选项删除了我每次重新运行应用程序时创建的所有保存的文件。

那些
catch(Exception)
子句闻起来真糟糕。试着检查这些异常,看看它们是抛出的还是什么,在任何情况下,修复它们,使其不那么一般,数据丢失。抛出的异常是一个FileNotFound异常。我遇到的问题是,Microsoft没有告诉我这些方法可能抛出什么异常,所以我只是在猜测。我现在使用Exception,当异常在单元测试期间逐渐出现时,我添加了更多特定异常的捕获。对于I/O内容,IOException将非常常见。但通常情况下,让你的程序在测试中崩溃并死亡会更好,因为它根本无法处理意外的异常,而不是吞下异常并记住记录它并放入正确的
catch
子句。我不反对,但我的问题是为什么加载文件会抛出FileNotFound异常,而不是我如何捕获异常。尽管如此,还是要感谢你的意见
private const string tokenFilename = "UserSession.dat";

private const string tokenKey = "UserToken";
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Lifestream.App.Mobile.WindowsApp.Services.FileStorageService.<LoadValueFromKey>d__16.MoveNext()