2012-05-23 106 views

回答

10

可以將文件夾視爲具有特殊MIME類型的文件:「application/vnd.google-apps.folder」。

下面的C#代碼應該是你所需要的:

File body = new File(); 
body.Title = "document title"; 
body.Description = "document description"; 
body.MimeType = "application/vnd.google-apps.folder"; 

// service is an authorized Drive API service instance 
File file = service.Files.Insert(body).Fetch(); 

有關詳情,請文檔:https://developers.google.com/drive/folder

+0

我無法創建文件夾,也沒有收到錯誤。而且.Fetch()方法不適合我? 如何實例化driveservice?我正在使用它像這樣 DriveService ds = new DriveService(); ds.Key = PicasaAuthToken; 謝謝。 – Sujit

+0

請檢查文檔以瞭解如何執行身份驗證(https://developers.google.com/drive/apps_overview),Picasa身份驗證令牌對雲端硬盤無效。 –

0
//First you will need a DriveService: 

ClientSecrets cs = new ClientSecrets(); 
cs.ClientId = yourClientId; 
cs.ClientSecret = yourClientSecret; 

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
         cs, 
         new[] { DriveService.Scope.Drive }, 
         "user", 
         CancellationToken.None, 
         null 
        ).Result; 

DriveService service = new DriveService(new BaseClientService.Initializer() 
       { 
        HttpClientInitializer = credential, 
        ApplicationName = "TheAppName" 
       }); 

//then you can upload the file: 

File body = new File(); 
body.Title = "document title"; 
body.Description = "document description"; 
body.MimeType = "application/vnd.google-apps.folder"; 

File folder = service.Files.Insert(body).Execute(); 
0

在谷歌雲端硬盤API,一個文件夾是什麼,但使用MIME文件類型:application/vnd.google-apps.folder

在API第2版,您可以使用:

// DriveService _service: Valid, authenticated Drive service 
    // string_ title: Title of the folder 
    // string _description: Description of the folder 
    // _parent: ID of the parent directory to which the folder should be created 

public static File createDirectory(DriveService _service, string _title, string _description, string _parent) 
{ 
    File NewDirectory = null; 

    File body = new File(); 
    body.Title = _title; 
    body.Description = _description; 
    body.MimeType = "application/vnd.google-apps.folder"; 
    body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } }; 
    try 
    { 
     FilesResource.InsertRequest request = _service.Files.Insert(body); 
     NewDirectory = request.Execute(); 
    } 
    catch(Exception e) 
    { 
     MessageBox.Show(e.Message, "Error Occured"); 
    } 
    return NewDirectory; 
} 

要在根目錄下創建文件夾,可以將"root"作爲父ID。

相關問題