【发布时间】:2017-05-05 18:43:08
【问题描述】:
我正在四处寻找一种方法来完成这个简单的任务。我可能错过了一些东西。
我正在通过桌面应用程序从驱动器下载文件并将它们保存到本地主机。我有两个问题:
首先,我将 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.");
}
感谢您的帮助!
【问题讨论】:
标签: c# api google-drive-api drive