我正在尝试将字符串保存到我的临时文件夹中的文本文件中,但是出现此错误:

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


谢谢

附言这是C#控制台应用程序。

我的代码:

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
        }
    }

}

最佳答案

那是因为您缺少一个.dll
添加对此程序集的引用:
C:\ Program Files(x86)\ Reference Assembly \ Microsoft \ Framework.NETCore \ v4.5.1 \ System.Runtime.WindowsRuntime.dll

这对于桌面应用程序使用WinRT API的扩展方法“ GetAwaiter”是必要的。

07-24 13:14