簡體   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