如何使用.NET Graph SDK判断driveItem是否为文件夹?
判断DriveItem是否为文件夹的方法(.NET Graph SDK)
- 检查
Folder属性:文件夹类型的DriveItem会包含非空的Folder属性,文件(包括无扩展名文件)的该属性为null。代码示例:
// 假设已获取目标DriveItem实例driveItem bool isFolder = driveItem.Folder != null;
- 通过
FileSystemInfo.IsDirectory判断:如果请求时指定获取FileSystemInfo属性,可直接读取该布尔值判断。需注意请求时要显式选择该字段:
// 构造请求时包含必要属性 var driveItem = await graphClient.Drives["driveId"].Items["itemId"] .Request() .Select(item => new { item.Folder, item.FileSystemInfo }) .GetAsync(); bool isFolder = driveItem.FileSystemInfo?.IsDirectory ?? false;
- 类型转换判别:可尝试将
DriveItem转换为DriveItemFolder类型,成功则为文件夹:
bool isFolder = driveItem is DriveItemFolder;
注意:默认请求
DriveItem时不会返回Folder或FileSystemInfo属性,必须通过.Select()显式指定要获取的字段,否则相关属性会为null,无法正确判断。
内容的提问来源于stack exchange,提问作者Jan Martin
相关产品推荐
相关产品推荐

