Google Drive Folder sample

Source repository:

A Winforms application that is an equivalent of One Drive for Google Drive. It demonstrates the File On-Demand ShellBoost feature. It’s not currently exposing a Shell Namespace Extension, but the sample could be modified to integrate the code that can be found in the Physical Overview sample.

The sample only run on Windows 10 version 1709 (or “Fall Creators Update”) because the “Files On-Demand” only run on this version of Windows or higher.

Note: This is only a sample code, provided as is. Data loss may arise, so we recommend you use a Google Drive account where data is not important when testing this sample.

Steps to run the sample

1)Since the sample connects to Google Drive, it needs API credentials. There are many ways to integrate with Google Drive, but the sample uses OAuth 2. So it needs a Google Drive Secrets .json file. The easiest way is to go to the page and Enable the Drive API, as shown here:

Steps to run the sample - Picture 5

2)Once you have downloaded a Secrets .json file, compile and run the sample. By default, it should show you this:

Steps to run the sample - Picture 6

3)This is normal. Now, use the File / Configure Secrets File menu item and choose the .json file you have downloaded from the Google developers’ site. You should now see this:
Steps to run the sample - Picture 34

4)The Secrets .json file has been configured ok, so the sample can now connect to the Google Drive API, on behalf of a Google Drive account / user. There is no account synchronized yet, so use the File / Add Account menu item. You should see something like this:
Steps to run the sample - Picture 91

5)Enter your email, click Next, enter your password, and you should see the user Consent Screen, something like this:
Steps to run the sample - Picture 92

6)Accept (Allow). The dialog box should close itself and now you should see something like this:
Steps to run the sample - Picture 93

This log shows the sample is synchronizing the file it has found on the Google Drive of the added account with the local drive.

Once the program is started, it will, by default, synchronize files automatically:

from Google Drive to the local drive every 300 seconds;

from the local drive to Google Drive automatically.

Here is the Google Drive view of our account (it has 3 files and 2 folders):

Steps to run the sample - Picture 94

And here is our local drive after the first synchronization:

Steps to run the sample - Picture 95

As you can see all files are created, but they don’t take any space on the physical disk (beyond the metadata like its name and size), just like with Microsoft’s OneDrive. If you open a file (right click, context menu, open, or double-click), Windows will automatically require the sample to download the file. The sample will download it from Google Drive and the application associated with this file will open. Now the explorer view will show this:

Steps to run the sample - Picture 96

Note that if you had exited the sample and opened the file, Windows would have told you it cannot do this, as it needs the sample app to run to fetch data.

You can observe the icon has changed. It means the file is now downloaded and its content is physically stored on the disk. Now, even if the sample application is closed or does not run, the file can still be opened, like any other file on the disk.

Synchronization works the other way around. If you create a file in the local folder, it will be uploaded to the Google Drive, as soon as possible. Here is the view of the account Google Drive after a Word document as been created in the local folder:

Steps to run the sample - Picture 97

Some points to note

“Google Docs” (Document, Spreadsheet, Presentation, etc.) in Google Drive are special type of items (not files) because they don’t use end-user quota. Because of this, their size cannot be determined by the Google Drive API, because they just don’t have a size. So, the application only synchronizes these items from Google Drive to the local folder, and it creates a .url file (an “Internet Shortcut”) that points to the Google Doc link on the web, not the file with its content.

Errors (such as Sharing Violation errors) are often encountered in the application log. This is a sample that demonstrates a feature and it makes apparent the issues it encounters during the normal course of its operations. So, these errors are, in general, purely informative.

For storing Google Drive information locally, the sample use a 100% free and open source project called SQLNado : that allows a .NET program to easily use a SQLite database. Since Windows 10 provides SQLite native runtime in 32 and 64-bit, the program does not need SQLite native runtime to be deployed.

The sample supports multiple Google Drive accounts.

The sample currently only synchronizes owned (by the current logged in account) and non-Trashed (removed) items from Google Drive.