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

标签 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'

谢谢

附言这是 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/

相关文章:

c# - 标签在一定长度后无意中消失

c# - WPF 附加属性在设计时无效(xaml 编辑器)

javascript - 将状态从控件发送到框​​架

oop - parking 场,OOP 设计 - 定制设计

ios - iOS View TableViewCell 中的面向对象编程问题

c# - 比较两个数组

c# - 在 C# 中引用的 Mono.Cecil 中的以下构造是什么?

javascript - 检测多张图片加载事件

mysql - 记录 mysql 查询

ExtJS 4 - 打开窗口时使用加载掩码在 IE8 和 IE9 中给出错误