如何使用.NET API在Google云端硬盘中创建文件夹?

时间:2012-05-23 08:12:20

标签: c# .net google-drive-api

我正在使用C#,Google .NET API。如何在Google云端硬盘根位置创建文件夹?任何代码都会有所帮助。感谢

3 个答案:

答案 0 :(得分:10)

可以将文件夹视为具有特殊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();

有关详细信息,请查看文档:{​​{3}}

答案 1 :(得分: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();

答案 2 :(得分:0)

在Google Drive API中,文件夹只是一个Mime Type: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传递。