2016-05-19 24 views
4

ドライブAPIを使用してgetリクエストから単一のファイル名を取得するにはどうすればよいですか?GoogleドライブV3 Api getファイル名、C#

私はリクエストしましたが、そこにはファイルに関するメタデータがありません。ダウンロードすることしかできません。

var fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M"; 
var request = driveService.Files.Get(fileId); 

は、どうやらこれは私が単にファイルをダウンロードしたいと、その名前が表示されている。このdoc

に応じて対応してfiles.getを返すだけではなく、そのID

+0


はこちらをご覧:http://stackoverflow.com/questions/23063691/how-to-get-file-name-and-real-path-of-google-drive-document –

答えて

0

はこの

をお試しください
/// 
    /// Download a file 
    /// Documentation: https://developers.google.com/drive/v2/reference/files/get 
    /// 
    /// a Valid authenticated DriveService 
    /// File resource of the file to download 
    /// location of where to save the file including the file name to save it as. 
    /// 
    public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo) 
    { 

     if (!String.IsNullOrEmpty(_fileResource.DownloadUrl)) 
     { 
      try 
      { 
       var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl); 
       byte[] arrBytes = x.Result; 
       System.IO.File.WriteAllBytes(_saveTo, arrBytes); 
       return true;     
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("An error occurred: " + e.Message); 
       return false; 
      } 
     } 
     else 
     { 
      // The file doesn't have any content stored on Drive. 
      return false; 
     } 
    } 

私のGoogle drive api C# downloadチュートリアルからコードがリッピングされました。私は何歳で更新していないので、何か問題がある場合は私に知らせて、私はそれらを修正します。

+0

V3 APIコールとV2 APIコールを混在させることはできますか? – FillyPajo

+0

実際には理想的ではありませんが、私はv2に似ているはずのv3コードを見つけようとします。読んで私の怠惰については申し訳ありません私は新しいapiについて忘れています – DaImTo

1

あなたはFileクラスのTitleプロパティからファイル名を取得できます。

string FileName = service.Files.Get(FileId).Execute().Title; 

およびダウンロードするため、GoogleドライブV3については

// DriveService _service: a valid Authendicated DriveService 
// Google.Apis.Drive.v2.Data.File _fileResource: Resource of the file to download. (from service.Files.Get(FileId).Execute();) 
// string _saveTo: Full file path to save the file 

public static void downloadFile(DriveService _service, File _fileResource, string _saveTo) 
{ 
    if (!String.IsNullOrEmpty(_fileResource.DownloadUrl)) 
    { 
     try 
     { 
      var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl); 
      byte[] arrBytes = x.Result; 
      System.IO.File.WriteAllBytes(_saveTo, arrBytes); 
     } 
     catch(Exception e) 
     { 
      MessageBox.Show(e.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      Environment.Exit(0); 
     } 
    } 
} 
1

のC#:
string f = driveService.Files.Get(fileId).Execute().Name;

VB: Dim f As String = driveService.Files.Get(fileId).Execute().Name

関連する問題