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

时间:2017-01-09 12:55:17

标签: c# .net google-api google-drive-api google-api-dotnet-client

我使用google drive api获取list(Google.Apis.Drive.v3.Data.File)调用文件。

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

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

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

1 个答案:

答案 0 :(得分:1)

所有file.list正在为您提供Google驱动器上文件的元数据列表。如果你想阅读文件的内容,你需要下载文件localy。

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的代码

相关问题