List Down Your Google Drive Files Using .NET


The Google Drive platform provides a list of APIs, along with client libraries, language-specific examples, and documentation for developers to integrate Drive functionalities with Web and Mobile applications. Core functionalities of Google Drive are to download and upload. However, the Drive also provides searching, sharing and collaboration, file listing, and exporting functions.

In this article, I will demonstrate to you, with an example of a C# console application, how to connect to your own personal Google Drive account and gather a list of file names and display in the console.


To create and execute the sample console application that I will demonstrate, you'll need:

  • A Google account with Google Drive enabled
  • Google Drive API Setup
  • A few files added in Google Drive
  • Visual Studio 2015 or later version

API Version Information

Following are the details of Google Drive API Version v3. Next, we will set up that API.

API Version v3
API Rev 133
API Docs https://developers.google.com/drive/
Discovery Name Drive
Generated At 2018-10-04 01:59:14 UTC
C# Generator google-apis-code-generator
Generator Version 1.5.1 / c#=1.10.0
Generator Build 2018-05-04 17:28:03 UTC

Set Up Drive API

Following are the setup steps developers need to complete before you can use the previously mentioned API.

Step 1

Sign up for a Google account if you don't have one already. I already have one.

Step 2

Create a new Google API Console project from Google API Console, as shown in Figure 1.

Google API Console (Create New Project)
Figure 1: Google API Console (Create New Project)

Add project name and location, as you can see in Figure 2. Click the Create button; that will save the project in the API console.

Create New Project
Figure 2: Create New Project

Step 3

You will be taken to the following Project dashboard once the project is created. Project Dashboard provides detailed reports that help you monitor the usage and health of your project and APIs for Work application. Click the APIs; you will be redirected to the API selection screen, as shown in Figure 3.

Google API Console Dashboard
Figure 3: Google API Console Dashboard

Step 4

Click the Enable APIs and Services Tab, as demonstrated in Figure 4.

Enable API
Figure 4: Enable API

On the next page, a search Text box will appear. Enter 'Drive' and click Enter. Select Google Drive API and move to the next screen (see Figure 5).

Add Google Drive API
Figure 5: Add Google Drive API

Finally, Enable the Google Drive API, as shown in Figure 6.

Google Drive API Enable Success
Figure 6: Google Drive API Enable Success

Step 5

Once the API is Enabled, you will get to the screen shown in Figure 7.

Google Drive API Console
Figure 7: Google Drive API Console

Next, to generate the Authentication Token, you have to select the scope of your API/APIs. Examine Figure 8 and select the scopes applicable for you.

Google Drive API Select Scope
Figure 8: Google Drive API Select Scope

Step 6

Copy the API key/keys, as shown in Figure 9. A key will be required during execution of the C# console application.

The API keys
Figure 9: The API keys

Step 7

Sometimes, developers access Google Drive APIs by using a service account. You can create one, as depicted in Figure 10, if required.

Google Drive API Service Account
Figure 10: Google Drive API Service Account

Sample .NET Console Project

To demonstrate the Google Drive API functionalities, I have created a new .NET console application project in Visual Studio and saved it with the name GoogleDriveAPIProject (see Figure 11).

Visual Studio New Console Application
Figure 11: Visual Studio New Console Application

Next, from the NuGet Package Manager Console, select the package Google.Apis.Drive, as shown in Figure 12, and install it to the project created earlier.

Visual Studio Add NuGet Package
Figure 12: Visual Studio Add NuGet Package

Download the configuration file from the Google Console API dashboard and move the downloaded file to your working directory; ensure that it is named credentials.json. Add the credentials.json file to your solution.

Copy and Paste the following code in your program.cs file and Build and run the sample by clicking Start in the Visual Studio toolbar.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GoogleDriveAPIProject
   class Program

      static string[] DriveScopes =
         { DriveService.Scope.DriveReadonly };
      static string MyApplicationName =
         "Google Drive API for .NET Developers";

      static void Main(string[] args)
         UserCredential Usercredential;

         using (var stream = new FileStream("credentials.json",
            FileMode.Open, FileAccess.Read))
            string credPath = "token.json";
            Usercredential = GoogleWebAuthorizationBroker
               DriveScopes, "user", CancellationToken.None,
               new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " +

         var service = new DriveService(new
            HttpClientInitializer = Usercredential,
            ApplicationName = MyApplicationName,

         FilesResource.ListRequest MylistRequest =
         MylistRequest.PageSize = 10;
         MylistRequest.Fields = "nextPageToken, files(id, name)";

         IList<Google.Apis.Drive.v3.Data.File> files =
            MylistRequest.Execute() .Files;]
         if (files != null && files.Count > 0)
            foreach (var file in files)
               Console.WriteLine("{0} ({1})", file.Name, file.Id);
            Console.WriteLine("No files found in your
               Google Drive.");


During program execution, you will be asked to log in to Google account, if you are not already logged in. If you are logged in to multiple Google accounts, you will be asked to select one account to use for the authorization.

Finally, in the browser you will see list of files added in your Google drive.


I hope this article explained how to connect the Google Drive API from C#. To know more about Google APIs, read through this documentation.

That's all for today. Happy coding!

This article was originally published on October 8th, 2018

About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date