我试图将字符串保存到我的临时文件夹中的文本文件中,但出现此错误:
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 Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll
这是桌面应用使用 WinRT API 的扩展方法“GetAwaiter”所必需的。
关于c# - 使用 Windows.Storage 命名空间时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27965135/