[Automation] Download Files from Google Drive in C#

Level of Difficulty: Beginner.

There are multiple ways to download files from Google Drive, some of which require authenticaton and others that require lots of code. Here’s the simplest way to download the files, provided you’ve got the right file url.

var url = "https://drive.google.com/uc?export=download&id=<file_id>";
var path = "<add temp file path where file should be downloaded>";

using (var client = new HttpClient())
{
    using (var s = client.GetStreamAsync(url))
    {
        using (var fs = new FileStream(path, FileMode.OpenOrCreate))
        {
            s.Result.CopyTo(fs);
        }
    }
}

The above approach also works when writing to memory stream, instead of copying to a file stream, copy to a new MemoryStream variable.

Published by Jacqui Muller

I am an application architect and part time lecturer by current professions who enjoys dabbling in software development, RPA, IOT, advanced analytics, data engineering and business intelligence. I am aspiring to complete a PhD degree in Computer Science within the next three years. My competencies include a high level of computer literacy as well as programming in various languages. I am passionate about my field of study and occupation as I believe it has the ability and potential to impact lives - both drastically and positively. I come packaged with an ambition to succeed and make the world a better place.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: