如何在一个驱动器的特定文件夹中搜索项目?
How to search an item in specific folder in one drive?
我正在使用 Microsoft Graph 来操作 OneDrive 中的文件。我需要在特定文件夹中搜索文件,如果该文件存在,则删除该文件。
我正在使用以下代码搜索文件,它给出了整个驱动器的搜索结果。
var checkIfExists = this.graphClient
.Me
.Drive
.Search(item["FileName"].ToString())
.Request()
.GetAsync()
.Result;
我只需要在特定文件夹中搜索文件,例如仅在重复文件夹中。
由于 Drive 是代表用户的一个驱动器的顶级资源,因此它与称为 DriveItems 的其他项目有关系。驱动器项目可以是任何内容、文件、文件夹或存储在驱动器中的任何其他项目。
因此,要在驱动器中搜索特定文件,您可以发出请求;
var driveItems = await graphClient.Me.Drive.Root
.Search(<'{search-query}'>)
.Request()
.GetAsync();
这应该可以帮助您根据搜索查询获取 DriveItem,检索到 DriveItem 后,您可以根据项目 ID 提出删除请求;
await graphClient.Me.Drive
.Items[<"{item-id}">]
.Request()
.DeleteAsync();
更新:
根据查找文件和删除文件代码的帮助请求,我在下面提供了它供您参考。
var files = await graphClient.Me.Drive.Root
.Search("abc.pdf")
.Request()
.GetAsync();
var duplicateFile = files
.Where(driveItem => driveItem.ParentReference.Name
.ToLower() == "duplicate")
.FirstOrDefault();
if(duplicateFile != null) {
await graphClient.Me.Drive
.Items[duplicateFile.Id]
.Request()
.DeleteAsync();
}
您可以将搜索范围限定在您喜欢的任何路径。例如,使用默认的 Graph Explorer 数据集,我们可以使用以下查询在整个云端硬盘中搜索 finance
:
https://graph.microsoft.com/v1.0/me/drive/root/search(q='finance')?select=name,id,webUrl
如果我们只想在单个子文件夹下搜索(例如/CR-227 Project/
),那么我们可以使用该路径作为起点:
https://graph.microsoft.com/v1.0/me/drive/root:/CR-227 Project:/search(q='finance')?select=name,id,webUrl
此外,如果我们知道 /CR-227 Project/
(01BYE5RZ6TAJHXA5GMWZB2HDLD7SNEXFFU
) 的 DriveItem.Id
,那么我们可以使用该 ID 而不是路径:
https://graph.microsoft.com/v1.0/me/drive/items/01BYE5RZ6TAJHXA5GMWZB2HDLD7SNEXFFU/search(q='finance')?select=name,id,webUrl
我正在使用 Microsoft Graph 来操作 OneDrive 中的文件。我需要在特定文件夹中搜索文件,如果该文件存在,则删除该文件。
我正在使用以下代码搜索文件,它给出了整个驱动器的搜索结果。
var checkIfExists = this.graphClient
.Me
.Drive
.Search(item["FileName"].ToString())
.Request()
.GetAsync()
.Result;
我只需要在特定文件夹中搜索文件,例如仅在重复文件夹中。
由于 Drive 是代表用户的一个驱动器的顶级资源,因此它与称为 DriveItems 的其他项目有关系。驱动器项目可以是任何内容、文件、文件夹或存储在驱动器中的任何其他项目。
因此,要在驱动器中搜索特定文件,您可以发出请求;
var driveItems = await graphClient.Me.Drive.Root
.Search(<'{search-query}'>)
.Request()
.GetAsync();
这应该可以帮助您根据搜索查询获取 DriveItem,检索到 DriveItem 后,您可以根据项目 ID 提出删除请求;
await graphClient.Me.Drive
.Items[<"{item-id}">]
.Request()
.DeleteAsync();
更新:
根据查找文件和删除文件代码的帮助请求,我在下面提供了它供您参考。
var files = await graphClient.Me.Drive.Root
.Search("abc.pdf")
.Request()
.GetAsync();
var duplicateFile = files
.Where(driveItem => driveItem.ParentReference.Name
.ToLower() == "duplicate")
.FirstOrDefault();
if(duplicateFile != null) {
await graphClient.Me.Drive
.Items[duplicateFile.Id]
.Request()
.DeleteAsync();
}
您可以将搜索范围限定在您喜欢的任何路径。例如,使用默认的 Graph Explorer 数据集,我们可以使用以下查询在整个云端硬盘中搜索 finance
:
https://graph.microsoft.com/v1.0/me/drive/root/search(q='finance')?select=name,id,webUrl
如果我们只想在单个子文件夹下搜索(例如/CR-227 Project/
),那么我们可以使用该路径作为起点:
https://graph.microsoft.com/v1.0/me/drive/root:/CR-227 Project:/search(q='finance')?select=name,id,webUrl
此外,如果我们知道 /CR-227 Project/
(01BYE5RZ6TAJHXA5GMWZB2HDLD7SNEXFFU
) 的 DriveItem.Id
,那么我们可以使用该 ID 而不是路径:
https://graph.microsoft.com/v1.0/me/drive/items/01BYE5RZ6TAJHXA5GMWZB2HDLD7SNEXFFU/search(q='finance')?select=name,id,webUrl