Get folder hierarchy with Google Drive API [C# / .NET]

Viewed 6176

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

Currently, you can get the root folder and its parents by

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

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.

3 Answers
Get folder hierarchy with Google Drive API [C# / .NET]

Google.Apis.Drive.v3.DriveService service = GetService();
List<GoogleDriveFile> folderList = new List<GoogleDriveFile>();

Google.Apis.Drive.v3.FilesResource.ListRequest request = service.Files.List();
//https://developers.google.com/drenter code hereive/api/v3/search-shareddrives
request.Q = string.Format("mimeType='application/vnd.google-apps.folder' and '{0}' in parents", folderId)`enter code here`;
request.Fields = "files(id, name)";

Google.Apis.Drive.v3.Data.FileList result = request.Execute();
foreach (var file in result.Files)
{
   GoogleDriveFile googleDriveFile = new GoogleDriveFile
   {
       Id = file.Id,
       Name = file.Name,
       Size = file.Size,
       Version = file.Version,
       CreatedTime = file.CreatedTime,
       Parents = file.Parents
   };
folderList.Add(googleDriveFile);               
}
return folderList;
Related