我正在四處搜尋試圖找到一種方法來完成這個簡單的任務..我可能錯過了一些東西。Google Drive API v3在特定文件夾中查找文件
我正在通過桌面應用程序從驅動器下載文件並將它們保存到本地主機。我有兩個問題:
首先,我使用y驅動器的幾件事情,所以我只想下載包含在特定文件夾中的文件。我需要一種方法來確定文件是否在文件夾中,以便我可以決定是否下載它。
這是我迄今爲止...
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
其次,我已經找到一種方法來下載文件,但我不知道是他們一旦下載完成。如何讓應用程序將文件保存到本地主機上的特定位置?這是我用於下載的代碼...
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.");
}
謝謝你的幫助!