如何在 C# 中读取 (Google.Apis.Drive.v3.Data.File)?

How to read from a (Google.Apis.Drive.v3.Data.File) in C#?

我使用 google 驱动器 api 获得了 list 个 (Google.Apis.Drive.v3.Data.File) 调用文件。

然后我使用循环和 if 语句来获取文本文件。

foreach (var file in files)
    {
        if (file.Name.Equals("UTMIDS.txt"))
        {
            //read data from file
        }
    }

如何读取该文件?! 提前致谢。

file.list 所做的只是为您获取 Google 驱动器上文件的元数据列表。如果您想阅读文件的内容,您需要将文件下载到本地。

var fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
var request = driveService.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 +=
    (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);

代码来自 Download Files