如何解决用于 StorageFolder.GetFolderFromPathAsync 的 CS4036
我正在编写 UWP 应用(Windows 10、Visual Studio 2019)。我想在这个应用程序中显示驱动器。 为了做到这一点,我创建了另一个较小的 UWP 应用程序,以便仅显示驱动器,然后尝试将代码放入更大的 UWP 应用程序中。在这两个应用程序中,我都启用了清单文件中的设置。
小型 UWP 应用有效:
using System;
using System.Diagnostics;
using System.IO;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender,RoutedEventArgs e)
{
const string FREE_SPACE = "System.FreeSpace";
const string TOTAL_SPACE = "System.Capacity";
DriveInfo[] drives = DriveInfo.GetDrives();
foreach(DriveInfo d in drives)
{
try
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(d.RootDirectory.FullName);
var props = await folder.Properties.RetrievePropertiesAsync(new string[] { FREE_SPACE,TOTAL_SPACE });
Debug.WriteLine("free space is:" + (UInt64)props[FREE_SPACE]);
Debug.WriteLine("capacity is: " + (UInt64)props[TOTAL_SPACE]);
}
catch(Exception e)
{
Debug.WriteLine(e.Message);
}
}
}
}
}
大型 UWP 应用中的代码:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using Windows.Storage;
using System.Diagnostics;
namespace MyNamespace
{
public class MyClass
{
private static async Task<IEnumerable<Drives>> DrivesAsync()
{
List<Drive> drivesList= new List<Drive>();
const string FREE_SPACE = "System.FreeSpace";
const string TOTAL_SPACE = "System.Capacity";
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
try
{
StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(d.RootDirectory.FullName);
var properties = await storageFolder.Properties.RetrievePropertiesAsync(new string[] { FREE_SPACE,TOTAL_SPACE });
drivesList.Add(new Drive
{
freeSpace = (UInt64)properties[FREE_SPACE],totalSpace = (UInt64)properties[TOTAL_SPACE]
});
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
return drivesList;
}
public static async Task<IEnumerable<Drive>> GetDrivesAsync()
{
await Task.CompletedTask;
return await DrivesAsync();
}
}
}
问题出在更大的应用程序中
StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(d.RootDirectory.FullName);
var properties = await storageFolder.Properties.RetrievePropertiesAsync(new string[] { FREE_SPACE,TOTAL_SPACE });
我有以下错误:
CS4036“IAsyncOperation”不包含“GetAwaiter”的定义,并且找不到接受“IAsyncOperation”类型的第一个参数的扩展方法“GetAwaiter”(您是否缺少用于“系统'?)
可以看出,文件顶部有using System;。
为什么更大的应用程序中的代码不起作用?我怎样才能摆脱错误?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。