Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive V3 Api get File Name, C#

How do I get a single files name from a get request using the drive api?

I have made a request but there is not metadata about the file there, I can only download it.

var fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
var request = driveService.Files.Get(fileId);

Apparently this return a files.get in the response according to this doc

I just want to download a file and have its name displayed, not just its id

like image 670
FillyPajo Avatar asked May 19 '16 21:05

FillyPajo


2 Answers

For Google Drive V3:

C#:
string f = driveService.Files.Get(fileId).Execute().Name;

VB:
Dim f As String = driveService.Files.Get(fileId).Execute().Name

like image 182
Fadi Avatar answered Sep 27 '22 18:09

Fadi


You can get the file name from the Title property in the File class:

string FileName = service.Files.Get(FileId).Execute().Title;

and for downloading,

// DriveService _service: a valid Authendicated DriveService
// Google.Apis.Drive.v2.Data.File _fileResource: Resource of the file to download. (from service.Files.Get(FileId).Execute();)  
// string _saveTo: Full file path to save the file

public static void downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
    if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
    {
        try
        {
            var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl);
            byte[] arrBytes = x.Result;
            System.IO.File.WriteAllBytes(_saveTo, arrBytes);
        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Environment.Exit(0);
        }
    }
}
like image 39
Divins Mathew Avatar answered Sep 27 '22 19:09

Divins Mathew