How to create a folder in Google Drive using the .NET API?

I am using C #, the Google.NET API. How do I create a folder in the root directory of Google Drive? Any code would be helpful. Thanks

+5
source share
3 answers

A folder can be processed as a file with a special MIME type: "application / vnd.google-apps.folder".

The following C # code should be what you need:

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

See the docs for more details: https://developers.google.com/drive/folder

+10
source
//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();
0
source

API Google - , 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" .

0

All Articles