Reputation: 151
I'm searching around trying to find a way to accomplish this simple task.. I'm probably missing something.
I am downloading files off of the drive via a desktop application and saving them to the localhost. I have two problems:
First, I use y drive for several things so I only want to download the files contained within a specific folder. I need a way to determin if a file is in the folder so I can decide whether or not to download it.
this is what I have so far...
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 100;
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Name, file.Id);
if (file.Name == "APIDSWelcome.txt")
{
id = file.Id;
if (file.MimeType == "application/vnd.google-apps.folder")
{
}
}
}
foreach (var file in files)
{
if (file.Parents.Contains(id))
{
//download
Second, I have found a way to download the files but I am not sure were they are once the download is complete. How do I tell the application to save the files to a specific location on the local host? This is the code I am using for downloading...
var fileId = file.Id;
var request = service.Files.Get(fileId);
var stream = new System.IO.MemoryStream();
// Add a handler which will be notified on progress changes.
// It will notify on each chunk download and when the
// download is completed or failed.
request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
break;
}
case DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.Download(stream);
}
}
}
else
{
Console.WriteLine("No files found.");
}
Thank you for the help!
Upvotes: 1
Views: 5314
Reputation: 72
You download into the
request.Download(stream)
then where does it go?
https://msdn.microsoft.com/en-us/library/sfezx97z(v=vs.110).aspx
This method allows you to open a dialog box so you can save the file where you want.
It looks like you're already checking to see if the folder is empty, or if it contains any thing with:
/ List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Name, file.Id);
if (file.Name == "APIDSWelcome.txt")
{
id = file.Id;
if (file.MimeType == "application/vnd.google-apps.folder")
{
}
}
}
This is where you should debug if you don;t think its working. Do you get results here? When you write the contents of the iList, what do you see? does it actually populate? If you got information, inserting a breakpoint.
Upvotes: 2