通过文件夹名称检查 Google Drive 中是不是存在文件夹(C#)
Posted
技术标签:
【中文标题】通过文件夹名称检查 Google Drive 中是不是存在文件夹(C#)【英文标题】:Check If Folder exist in Google Drive by Folder Name (C#)通过文件夹名称检查 Google Drive 中是否存在文件夹(C#) 【发布时间】:2020-07-01 14:20:21 【问题描述】:我初始化了 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;
【问题讨论】:
【参考方案1】:由于文件名在谷歌驱动器中不是唯一的,我们必须找到文件名以确定它是否存在。这是一个工作示例:
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;
参考:
List files .NET
【讨论】:
我面临以下问题:我的帐户中有一个共享驱动器。该共享驱动器包含一个文件夹“2020”,现在使用代码我需要在该文件夹“2020”内创建文件夹,我得到了 404 未找到的异常,但父 ID 存在,相同的代码正在创建文件夹在我的云端硬盘中。请提出建议。以上是关于通过文件夹名称检查 Google Drive 中是不是存在文件夹(C#)的主要内容,如果未能解决你的问题,请参考以下文章
为什么Google文档在使用Google Drive API时显示的尺寸为零?
如果Google Drive文件夹中已经存在相同名称的文件,则附加代码以覆盖PDF
通过 Google Drive API 从本地 CSV 文件创建 Google Drive 电子表格
如何通过 Google Drive 和 App Engine SDK 使用 Oauth2 服务帐户
用于将 google drive 中的文件名列表与 google sheet 列中的名称列表进行比较的 Apps 脚本,以将新文件从驱动器添加到工作表