在閱讀Gdrive.v3 SDK中Google上的遷移信息後:#https://developers.google.com/drive/v3/web/migration,我仍然不確定如何設置ParentReference(根目錄文件夾)爲正確的文件。在C#.Net中將Google Drive Apis v2的ParentReference移植到v3中
private string UploadToGDrive(Google.Apis.Drive.v3.DriveService service, string uploadFile, string parent)
{
var body = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(uploadFile),
Description = uploadFile,
MimeType = GetMimeType(uploadFile),
Parents = new List<ParentReference> {new ParentReference {Id = parent}}
};
}
private string GetMimeType(string fileName)
{
var mimeType = "application/unknown";
var extension = Path.GetExtension(fileName);
if (extension == null) return mimeType;
var ext = extension.ToLower();
var regKey = Registry.ClassesRoot.OpenSubKey(ext);
if (regKey?.GetValue("Content Type") != null) return mimeType;
if (regKey != null) mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}