谷歌驱动器API创建子文件夹 [英] google drive api create sub folders

查看:218
本文介绍了谷歌驱动器API创建子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在谷歌驱动器中创建子文件夹,使用在控制台应用程序中使用nuget包添加的谷歌驱动器api。

我可以获取根文件夹的文件夹ID。可以得到rot文件夹的孩子,也可以在根文件夹中上传文件。只有问题是创建文件夹中的子文件夹。

  for(int i = 1; i< array.Count(); i ++)
{
var subfoldername = new Google.Apis.Drive.v2.Data.File {Title = array [i],MimeType =application / vnd.google-apps.folder};
ChildrenResource.ListRequest request = service.Children.List(rootfolderid);
ChildList children = request.Execute();
if(children.Items.Count> 0)
{
foreach(Child.Items中的ChildReference c)
{
Google.Apis.Drive.v2。 Data.File file = service.Files.Get(c.Id).Execute();
if(file.MimeType ==application / vnd.google-apps.folder)
{
List< GoogleDriveFile> googledrive =新列表< GoogleDriveFile>();
googledrive.Add(new GoogleDriveFile
{
OriginalFilename = file.OriginalFilename
});
}
}
}
else
{
//这里需要在文件夹中添加子文件夹,但是这一行添加了根目录
var result = service.Files.Insert(foldername).Execute();


解决方案

,当在谷歌驱动器中创建一个子文件夹时,它必须需要父母。所以在执行字符串q之前,我们需要搜索父根id

  string findRootId =mimeType ='application / vnd .google-apps.folder'和title ='+ RootFolder +'和trashed = false; 

IList< File> _RootId = GoogleDriveHelper.GetFiles(service,findRootId);

if(_RootId.Count == 0)
{
_RootId.Add(GoogleDriveHelper.createDirectory(service,RootFolder,,root));
Console.WriteLine(创建根文件夹{0},RootFolder);
}
var id = _RootId [0] .Id;
$ b $ string Q =mimeType ='application / vnd.google-apps.folder'and'+ id +'in parents and title ='+ GoogleDriveFolderName +'and trashed = false ;


I need to create sub folders in google drive using google drive api added using nuget package in console application.

I can get the folder id of root folder. Can get children of rot folder, can also upload file in root folder. Only problem is creation of sub folders in folders.

for (int i = 1; i < array.Count(); i++)
                        {
                            var subfoldername = new Google.Apis.Drive.v2.Data.File { Title = array[i], MimeType = "application/vnd.google-apps.folder" };
                            ChildrenResource.ListRequest request = service.Children.List(rootfolderid);
                            ChildList children = request.Execute();
                            if (children.Items.Count > 0)
                            {
                                foreach (ChildReference c in children.Items)
                                {
                                    Google.Apis.Drive.v2.Data.File file = service.Files.Get(c.Id).Execute();
                                    if (file.MimeType == "application/vnd.google-apps.folder")
                                    {
                                        List<GoogleDriveFile> googledrive = new List<GoogleDriveFile>();
                                        googledrive.Add(new GoogleDriveFile
                                        {
                                            OriginalFilename = file.OriginalFilename
                                        });
                                    }
                                }
                            }
                            else
                            {
// here need to add sub folder in folder, but this line adds folder at root
                                var result = service.Files.Insert(foldername).Execute();
                            }

解决方案

Here is the way i do, when creating a sub folder in google drive it must need a parents. So before execute the string q, we need to search for the parent root id

        string findRootId = "mimeType = 'application/vnd.google-apps.folder' and title ='" + RootFolder + "' and trashed = false";

            IList<File> _RootId = GoogleDriveHelper.GetFiles(service, findRootId);

    if (_RootId.Count == 0)
                            {
                                _RootId.Add(GoogleDriveHelper.createDirectory(service, RootFolder, "", "root"));
                                Console.WriteLine("Root folder {0} was created.", RootFolder);
                            }
                            var id = _RootId[0].Id;

 string Q = "mimeType = 'application/vnd.google-apps.folder' and '" + id + "' in parents and title ='" + GoogleDriveFolderName + "' and trashed = false";

这篇关于谷歌驱动器API创建子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆