我正在使用 C#、Google .NET API。如何在 Google Drive 根目录中创建文件夹?任何代码都会有所帮助。谢谢
问问题
12641 次
3 回答
13
可以将文件夹视为具有特殊 MIME 类型的文件:“application/vnd.google-apps.folder”。
以下 C# 代码应该是您需要的:
File body = new File();
body.Title = "document title";
body.Description = "document description";
body.MimeType = "application/vnd.google-apps.folder";
// service is an authorized Drive API service instance
File file = service.Files.Insert(body).Fetch();
有关更多详细信息,请查看文档:https ://developers.google.com/drive/folder
于 2012-05-23T13:00:08.833 回答
0
//First you will need a DriveService:
ClientSecrets cs = new ClientSecrets();
cs.ClientId = yourClientId;
cs.ClientSecret = yourClientSecret;
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
cs,
new[] { DriveService.Scope.Drive },
"user",
CancellationToken.None,
null
).Result;
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "TheAppName"
});
//then you can upload the file:
File body = new File();
body.Title = "document title";
body.Description = "document description";
body.MimeType = "application/vnd.google-apps.folder";
File folder = service.Files.Insert(body).Execute();
于 2015-06-09T16:48:55.210 回答
0
在 Google Drive API 中,文件夹只不过是具有 Mime 类型的文件:application/vnd.google-apps.folder
在 API v2 中,您可以使用:
// DriveService _service: Valid, authenticated Drive service
// string_ title: Title of the folder
// string _description: Description of the folder
// _parent: ID of the parent directory to which the folder should be created
public static File createDirectory(DriveService _service, string _title, string _description, string _parent)
{
File NewDirectory = null;
File body = new File();
body.Title = _title;
body.Description = _description;
body.MimeType = "application/vnd.google-apps.folder";
body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
try
{
FilesResource.InsertRequest request = _service.Files.Insert(body);
NewDirectory = request.Execute();
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Error Occured");
}
return NewDirectory;
}
要在根目录创建文件夹,您可以"root"
作为父 ID 传递。
于 2016-12-25T06:50:26.837 回答