Ejemplo n.º 1
0
        //get all files from Google Drive.
        public static List <GoogleDriveFile> GetDriveFiles()
        {
            Google.Apis.Drive.v3.DriveService service = GetService();

            // Define parameters of request.
            Google.Apis.Drive.v3.FilesResource.ListRequest FileListRequest = service.Files.List();
            // for getting folders only.
            //FileListRequest.Q = "mimeType='application/vnd.google-apps.folder'";
            FileListRequest.Fields = "nextPageToken, files(*)";

            // List files.
            IList <Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;
            List <GoogleDriveFile> FileList = new List <GoogleDriveFile>();


            // For getting only folders
            // files = files.Where(x => x.MimeType == "application/vnd.google-apps.folder").ToList();


            if (files != null && files.Count > 0)
            {
                foreach (var file in files)
                {
                    GoogleDriveFile File = new GoogleDriveFile
                    {
                        Id          = file.Id,
                        Name        = file.Name,
                        Size        = file.Size,
                        Version     = file.Version,
                        CreatedTime = file.CreatedTime,
                        Parents     = file.Parents,
                        MimeType    = file.MimeType
                    };
                    FileList.Add(File);
                }
            }
            return(FileList);
        }
Ejemplo n.º 2
0
        public static List <GoogleDriveFile> GetDriveFolders()
        {
            Google.Apis.Drive.v3.DriveService service    = GetService();
            List <GoogleDriveFile>            FolderList = new List <GoogleDriveFile>();

            Google.Apis.Drive.v3.FilesResource.ListRequest request = service.Files.List();
            request.Q      = "mimeType='application/vnd.google-apps.folder'";
            request.Fields = "files(id, name)";

            Google.Apis.Drive.v3.Data.FileList result = request.Execute();
            foreach (var file in result.Files)
            {
                GoogleDriveFile File = new GoogleDriveFile
                {
                    Id          = file.Id,
                    Name        = file.Name,
                    Size        = file.Size,
                    Version     = file.Version,
                    CreatedTime = file.CreatedTime
                };
                FolderList.Add(File);
            }
            return(FolderList);
        }
Ejemplo n.º 3
0
        //Delete file from the Google drive
        public static void DeleteFile(GoogleDriveFile files)
        {
            Google.Apis.Drive.v3.DriveService service = GetService();
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }

                if (files == null)
                {
                    throw new ArgumentNullException(files.Id);
                }

                // Make the request.
                service.Files.Delete(files.Id).Execute();
            }
            catch (Exception ex)
            {
                throw new Exception("Request Files.Delete failed.", ex);
            }
        }