-2

I Want upload Zip file on Google Drive but using C Sharp Windows Application (Frame Work 4.0 VS 2010)

enter image description here

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                                   GoogleClientSecrets.Load(stream).Secrets,
                                   Scopes,
                                   "user",
                                   CancellationToken.None,
                                   new FileDataStore(credPath, true)).Result;
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

1 Answers1

0

The Google .Net client library does still partially support .net 4.0

  private static string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }        

        public static Google.Apis.Drive.v3.Data.File uploadFile(Google.Apis.Drive.v3.DriveService service, string uploadFile, string[] parents)
        {
            if (System.IO.File.Exists(uploadFile))
            {
                var body = new Google.Apis.Drive.v3.Data.File();
                body.Name = System.IO.Path.GetFileName(uploadFile);
                body.Description = "File uploaded by Diamto Drive Sample";
                body.MimeType = GetMimeType(uploadFile);
                body.Parents = parents;

                // File's content.
                byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                try
                {
                    var request = service.Files.Create(body, stream, GetMimeType(uploadFile));
                    request.Upload();
                    return request.ResponseBody;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message); return null;
                }
            }
            else { Console.WriteLine("File does not exist: " + uploadFile); return null; }
        }

Code was altered from my tutorial for Drive v2 Google drive upload Note this is not resumeable upload you will need to work that out yourself.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • No actually i got error when i execute the program "Aggregate Exception" – Pradip Bandal May 31 '17 at 10:27
  • Tell me one thing is it possible using drive v3 api in Visual Studio 2010 Framework 4.0 – Pradip Bandal May 31 '17 at 10:30
  • Yes its possible you can use Google apis with any language that supports http post and http get. The Google .Net client library will make it easer for you and it does support .net 4.0 You can also use the .net client library install the nuget package. Try this you will have to alter it for v3 though http://www.daimto.com/google-drive-api-c-upload/ – Linda Lawton - DaImTo May 31 '17 at 10:41
  • how to remove v3 drive api? – Pradip Bandal May 31 '17 at 10:45
  • You dont remove Drive v3 that's Google api you have no control over that. – Linda Lawton - DaImTo May 31 '17 at 10:46
  • Unable to read package from path 'Microsoft.Bcl.1.1.8\Microsoft.Bcl.1.1.8.nupkg i got error – Pradip Bandal May 31 '17 at 10:51
  • PM> Install-Package Google.Apis.Drive.v2 Attempting to resolve dependency 'Google.Apis (= 1.10.0)'. Attempting to resolve dependency 'Google.Apis.Core (≥ 1.10.0)'. Attempting to resolve dependency 'Microsoft.Bcl (≥ 1.1.10)'. Install-Package : Unable to read package from path 'Microsoft.Bcl.1.1.8\Microsoft.Bcl.1.1.8.nupkg'. At line:1 char:16 + Install-Package <<<< Google.Apis.Drive.v2 + CategoryInfo : NotSpecified: (:) [Install-Package], InvalidDataException + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand – Pradip Bandal May 31 '17 at 10:53
  • The code above is for V3 which I just tested in a console application running .net 4.0. If you want to use V2 then you should follow the original tutorial. – Linda Lawton - DaImTo May 31 '17 at 11:03
  • Sorry for inconvenience – Pradip Bandal May 31 '17 at 11:17