使用Windows.Storage命名空间时出错

时间:2015-01-15 14:02:38

标签: c# oop load save

我正在尝试将字符串保存到我的临时文件夹中的文本文件中但是我有这个错误:

Error2'await' requires that the type 'Windows.Foundation.IAsyncAction' have a 
suitable GetAwaiter method. Are you missing a using directive for 'System'

由于

P.S。这是C#console app。

我的代码:

using System;
using System.IO;
using System.Linq;
using Windows.Storage;

public static class Storage
{

    public static async void SaveData()
    {
        string myString = "This is the data I want to save";
        ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

        // Add:  using Windows.Storage;
        Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder;

        // Optionally overwrite any existing file with CreationCollisionOption
        StorageFile file = await localFolder.CreateFileAsync("mySaveFile.txt", CreationCollisionOption.ReplaceExisting);

        try
        {
            if (file != null)
            {
                await FileIO.WriteTextAsync(file, myString);
            }
        }
        catch (FileNotFoundException)
        {
            // Error saving data
        }
    }

}

1 个答案:

答案 0 :(得分:2)

那是因为你错过了.dll

添加对此程序集的引用:
C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETCore \ v4.5.1 \ System.Runtime.WindowsRuntime.dll

桌面应用程序必须使用WinRT API中的扩展方法“GetAwaiter”。