Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Google Drive API.NET的嵌套文件夹查找_C#_Google Drive Api_Google Api Dotnet Client - Fatal编程技术网

C# Google Drive API.NET的嵌套文件夹查找

C# Google Drive API.NET的嵌套文件夹查找,c#,google-drive-api,google-api-dotnet-client,C#,Google Drive Api,Google Api Dotnet Client,我已经在GoogleDriveV3中使用自己的GoogleAPI for.NET实现了文件/文件夹查找 代码工作得很好,但老实说,我不确定它是否真的是基于一种标准的高效方式 逻辑: 我必须进入每个文件夹,下载其中的特定文件 结构可以是A>B>C>D,基本上是一个文件夹中的一个文件夹中的一个文件夹,以此类推 我不能使用静态预定义目录模式作为长期解决方案,因为它可以在所有者想要修改它的任何时候进行更改,目前,文件夹至少有4级深度 我可以导航到子文件夹的唯一方法是获取它自己的Google驱动器ID,并

我已经在GoogleDriveV3中使用自己的GoogleAPI for.NET实现了文件/文件夹查找

代码工作得很好,但老实说,我不确定它是否真的是基于一种标准的高效方式

逻辑:

  • 我必须进入每个文件夹,下载其中的特定文件

  • 结构可以是A>B>C>D,基本上是一个文件夹中的一个文件夹中的一个文件夹,以此类推

  • 我不能使用静态预定义目录模式作为长期解决方案,因为它可以在所有者想要修改它的任何时候进行更改,目前,文件夹至少有4级深度

  • 我可以导航到子文件夹的唯一方法是获取它自己的Google驱动器ID,并使用该ID查看其内容。这就像在解锁/打开下一个子文件夹之前,您首先需要一把钥匙
  • 简而言之,我不能简单地查找子文件夹内容,除非有人能给我们提供更好的选择,否则我很乐意接受对我的建议的任何批评,并接受你的所有建议

    多谢各位

    更新 感谢大家提供链接和示例, 我相信递归解决方案是目前为止我的方案中最好的, 而且,由于这是一个繁重的IO操作,我确实为下载文件应用了异步操作,并尽可能地将其应用于其他操作,因此我确保始终遵循异步规则以防止阻塞

    调用递归方法

    var parentID = "<folder id>";
    var folderLevel = 0;
    var listRequest = service.Files.List();
    await MyTask(listRequest, id, count, folderLevel);
    

    我用PageStreamer@daimt做了一些类似的事情。非常感谢,我看到了你的页面,看起来PageStreamer是一个很有前途的页面。如果你有任何问题,请告诉我。我现在已经整理好了,感谢大家的输入和示例。
    private async Task RecursionTask(ListRequest listRequest, string parentId, int count, int folderLevel)
    {
        // This method do the Folder search
        listRequest.Q = $"('{parentId}' in parents) and (mimeType = 'application/vnd.google-apps.folder') and trashed = false";
        listRequest.Fields = "files(id,name)";
    
        var filesTask = await listRequest.ExecuteAsync();
        var files = filesTask.Files;
    
        count = files.Count(); // Keep track of recursion flow
        count--;
    
        // Keep track of how deep recursion is diving on subfolders
        folderLevel++; 
    
        var tasks = new List<Task>();
        foreach(var file in files)
        {
            tasks.Add(InnerTask(file, listRequest, name, folderLevel)); // Create Array Of Tasks for IMAGE Search
            if (count > 1)  // Loop until I exhausted the value of count
            {
                // Return recursion flow
                await RecursionTask(listRequest, file.Name, file.Id, count, folderLevel);  
            }
        }
        await Task.WhenAll(tasks); // Wait all tasks to finish
    }
    
    private async Task InnerTask(File file, ListRequest listRequest, string name,int folderLevel)
    {
        // This method do the IMAGE SEARCH 
        listRequest.Q = $"('{file.Id}' in parents) and (mimeType = 'image/jpeg' or mimeType = 'image/png')";
        listRequest.Fields = "files(id,name)";
    
        var subFiles = await listRequest.ExecuteAsync();
        foreach (var subFile in subFiles.Files)
        {
            // Do Async task for downloading images on Google Drive
        }
    }