Google Drive V3 Api gets file name, C #

How to get one file name from a request to receive using api disk?

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

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

Apparently this returns the .get file in response according to this doc

I just want to upload a file and show its name, not just its id

+4
source share
3 answers

You can get the file name from a property Titlein the class File:

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

and for download,

// 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);
        }
    }
}
+1
source

For Google Drive V3:

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

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

+1
source

    /// 
    /// Download a file
    /// Documentation: https://developers.google.com/drive/v2/reference/files/get
    /// 
    /// a Valid authenticated DriveService
    /// File resource of the file to download
    /// location of where to save the file including the file name to save it as.
    /// 
    public static Boolean 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);
                return true;                  
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                return false;
            }
        }
        else
        {
            // The file doesn't have any content stored on Drive.
            return false;
        }
    }

, Google api # download. , , - , , .

0
source

All Articles