【问题标题】:existing file in subfolder google drive子文件夹 google drive 中的现有文件
【发布时间】:2018-01-15 16:31:02
【问题描述】:

我想用 vb.net 验证文件是否存在于谷歌驱动器中,文件路径在子文件夹中。我创建了 existsfile 方法,其中列出了子文件夹和列表文件并使用了测试,但总是 countfile=0 。 这是existfile方法代码

 Public Function existfile(v As String) As String
    Dim page As String = ""
    Dim req = Service.Files.List()
    req.Q = "mimeType = 'application/vnd.google-apps.file' and trashed = false "
    req.Spaces = "drive"
    req.Fields = "nextPageToken, items(id, title)"
    req.PageToken = page
    Dim result = req.Execute()
    ' sous doc
    Dim ref = Service.Files.List()
    ref.Q = "mimeType = 'application/vnd.google-apps.folder' and trashed = false "
    Dim fo = ref.Execute()

    'récuprer la reference (ID) du dossier existant , sinon la fonction retourne false en cas d'inexistance'
    Const a As String = "false"
    For Each listf In fo.Items
        For Each test In result.Items
            If (test.Title = v) Then
                Return (test.Id)
            End If
        Next
    Next
    Return (a)
End Function

每个的第一个是列表文件夹,第二个是列表文件已经存在,但result.item总是0

文件路径在子文件夹下,子文件夹在大文件夹下

【问题讨论】:

    标签: vb.net file google-drive-api exists


    【解决方案1】:

    您可能需要检查此.NET Quickstart,它使用Files.List 列出您 Google 云端硬盘上的文件。

    // Define parameters of request.
    FilesResource.ListRequest listRequest = service.Files.List();
    listRequest.PageSize = 10;
    listRequest.Fields = "nextPageToken, files(id, name)";
    
    // List files.
    IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
            .Files;
    Console.WriteLine("Files:");
    if (files != null && files.Count > 0)
    {
        foreach (var file in files)
        {
                Console.WriteLine("{0} ({1})", file.Name, file.Id);
        }
    }
    else
    {
        Console.WriteLine("No files found.");
    }
    Console.Read();
    

    其他参考:Google Drive API - Check if file exists by file ID

    【讨论】:

    • 谢谢,您的代码在谷歌驱动器中搜索文件,而不是在文件夹或子文件夹中(我的意思总是结果是没有找到文件)。我找到了一个解决方案,我使用了2个foreach,第一个列出驱动器中存在的所有文件夹,下一个列出文件夹中存在的所有文件,然后我使用了一个if测试,结果它工作正常。
    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2017-06-04
    相关资源
    最近更新 更多