繁体   English   中英

按文件夹名称检查 Google Drive 中是否存在文件夹 (C#)

[英]Check If Folder exist in Google Drive by Folder Name (C#)

我初始化了 Google API 服务并将该服务传递给 Create folder 方法,每次它在 Google Drive 中创建相同的文件夹时,我想应用逻辑如果文件夹存在,不要创建它,只需返回它的 FolderID 否则创建新的文件夹并返回其文件夹 ID。 请对此提出建议。

  var service= InitializeGoogleDriveAPIService(clientId, clientSecret);
        var res=CreateFolder(service, "FolderName");


  private static DriveService InitializeGoogleDriveAPIService(string clientId, string clientSecret)
    {
        string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile, };
        ClientSecrets clientSecrets = new ClientSecrets();
        clientSecrets.ClientId = clientId;
        clientSecrets.ClientSecret = clientSecret;

        // Below is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%  
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, scopes, Environment.UserName, CancellationToken.None, new FileDataStore("MyAppsToken")).Result;

        //Once consent is received, the token will be stored locally on the AppData directory,
        //so that next time we won't be prompted for consent. -one time setting for the Console app   

        BaseClientService.Initializer initializer = new BaseClientService.Initializer();
        initializer.HttpClientInitializer = credential;
        initializer.ApplicationName = "PRInvoices";

        DriveService service = new DriveService(initializer);
        service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
        //Long Operations like file uploads might timeout. 100 is just precautionary value,
        //can be set to any reasonable value depending on what you use your service for  

        return service;
    }

  public static string CreateFolder(DriveService service, string folderName)
    {
        var file = new Google.Apis.Drive.v3.Data.File { Name = folderName, MimeType = "application/vnd.google-apps.folder" };

       // var RESILT = service.Files.List();

        var result = service.Files.Create(file).Execute();
        return result.Id;
    }

由于文件名在谷歌驱动器中不是唯一的,我们必须找到文件名来确定它是否存在。 这是一个工作示例:

public static string CreateFolder(DriveService service, string folderName)
        {
            var newFile = new Google.Apis.Drive.v3.Data.File { Name = folderName, MimeType = "application/vnd.google-apps.folder" };

            // 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;

            if (files != null && files.Count > 0)
            {
                foreach (var file in files)
                {
                    if(file.Name == newFile.Name) {
                        Console.WriteLine("File already existing... Skip creation");
                        return file.Id;
                    }
                }
            }
            else
            {
                Console.WriteLine("No files found.");
            }
            Console.WriteLine("Creating new file...");

            var result = service.Files.Create(newFile).Execute();

            return result.Id;
        }

参考:

列出文件 .NET

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM