google drive api 创建子文件夹 [英] google drive api create sub folders

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

问题描述

我需要使用在控制台应用程序中使用 nuget 包添加的 google drive api 在 google drive 中创建子文件夹.

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

我可以得到根文件夹的文件夹ID.可以获取 rot 文件夹的子文件夹,也可以上传根文件夹中的文件.唯一的问题是在文件夹中创建子文件夹.

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

推荐答案

这是我的做法,在谷歌驱动器中创建子文件夹时,它必须需要父母.所以在执行字符串q之前,我们需要搜索父根id

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";

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

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