使用 Google Drive API [C#/.NET] 获取文件夹层次结构 [英] Get folder hierarchy with Google Drive API [C# / .NET]

查看:27
本文介绍了使用 Google Drive API [C#/.NET] 获取文件夹层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种优雅的方式来获取文件夹层次结构,从我的根文件夹开始,使用 C# Google Drive API V3.

I am looking for an elegant way to get the folder hierarchy, beginning with my root folder, using the C# Google Drive API V3.

目前,您可以通过

var getRequest = driveService.Files.Get("root");
getRequest.Fields = "parents";
var file = getRequest.Execute();

但我正在寻找一种方法来获取孩子,而不是父母,所以我可以递归地查看文件结构.设置 getRequest.Fields = 'children' 不是有效的字段选项.

but I am looking for a way to get the children, not the parents, so I can recursively go down the file structure. Setting getRequest.Fields = 'children' is not a valid field option.

推荐答案

递归获取子级是获取完整层次结构的一种非常耗时的方式.更好的是运行查询以获取单个 GET 中的所有文件夹(如果您有 1,000 个以上的文件夹,则可能需要多个文件夹),然后遍历它们的父属性以在内存中构建层次结构.请记住,(afaik)没有什么可以阻止文件夹层次结构循环,因此文件夹 1 拥有文件夹 2 拥有文件夹 3 拥有文件夹 1,因此无论您采用哪种策略,请检查您是否处于循环中.

recursively fetching children is a very time consuming way to fetch the full hierarchy. Much better is to run a query to fetch all folders in a single GET (well it might take more than one if you have more than 1,000 folders) and then traverse their parent properties to build up the hierarchy in memory. Bear in mind that (afaik) there is nothing that prevents a folder hierarchy being cyclic, thus folder1 owns folder2 owns folder3 owns folder1, so whichever strategy you follow, check that you aren't in a loop.

如果您不熟悉 GDrive,请务必尽早意识到文件夹只是标签,而不是容器.因此,具有多个父级的循环关系和文件是很正常的.它们最初被称为 Collections,但更名为 Folders 以安抚那些无法理解标签的社区成员.

If you're new to GDrive, it's important to realise early on that Folders are simply labels, rather than containers. So cyclic relationships and files with multiple parents is quite normal. They were originally called Collections, but got renamed to Folders to appease those members of the community that couldn't get their head around labels.

这篇关于使用 Google Drive API [C#/.NET] 获取文件夹层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆