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

本文关键字:硬盘 创建 文件夹 云端 Google 何使用 NET API | 更新日期: 2023-09-27 18:34:57

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

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

文件夹

可以被视为具有特殊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

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

在 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 传递。

如果您希望完成它并添加父 ID:

    public static string Drive_FolderCreate(string ParentId, string FolderName) 
    {
        // create your credentials here with the scopes you want
        GoogleCredential Credential = ...
        DriveService service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = Credential, ApplicationName = "Aanmeldingen" });
        // Check if the folder already exists and return it's ID
        string MapID = Functies.Drive_FolderExists(ParentId, FolderName);
        if (!string.IsNullOrEmpty(MapID)) return MapID;
        Google.Apis.Drive.v3.Data.File Folder = new Google.Apis.Drive.v3.Data.File();
        Folder.Parents = new List<string>() { ParentId };
        Folder.Name = FolderName;
        Folder.MimeType = "application/vnd.google-apps.folder";
        var request = service.Files.Create(Folder);
        request.Fields = "id";
        request.SupportsAllDrives = true;
        request.SupportsTeamDrives = true;
        try
        {
            Google.Apis.Drive.v3.Data.File response = request.Execute();
            return response.Id;
        }
        catch { return null; }
    }

我希望它能为你们所有人解决问题。