簡體   English   中英

使用Windows.Storage命名空間時出錯

[英]Error while using Windows.Storage namespace

我試圖將字符串保存到我的臨時文件夾中的文本文件,但我有這個錯誤:

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

謝謝

PS這是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
        }
    }

}

那是因為你錯過了一個.dll

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

這是桌面應用程序使用WinRT API中的擴展方法“GetAwaiter”所必需的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM