salman
salman

Reputation: 35

getting a 'File' is an ambiguous reference error while insert file in google drive account

I am getting an error while insert file in google drive account using the below code

'File' is an ambiguous reference between 'Google.Apis.Drive.v2.Data.File' and 'System.IO.File'

 private static File insertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename)
        {
            // File's metadata.
            File body = new File();
            body.Title = title;
            body.Description = description;
            body.MimeType = mimeType;

            // Set the parent folder.
            if (!String.IsNullOrEmpty(parentId))
            {
                body.Parents = new List<ParentReference>() { new ParentReference() { Id = parentId } };
            }

            // File's content.
            byte[] byteArray = System.IO.File.ReadAllBytes(filename);
            MemoryStream stream = new MemoryStream(byteArray);

            try
            {
                FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
                request.Upload();

                File file = request.ResponseBody;

                // Uncomment the following line to print the File ID.
                // Console.WriteLine("File ID: " + file.Id);

                return file;
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                return null;
            }
        }

Upvotes: 0

Views: 2005

Answers (5)

Buzz
Buzz

Reputation: 6330

Google.Apis.Drive.v2.Data and System.IO both define File class, so use full name Google.Apis.Drive.v2.Data.File and System.IO.File

Upvotes: 2

Leri
Leri

Reputation: 12525

Both namespaces (Google.Apis.Drive.v2.Data and System.IO) defines class File and, probably, you have both namespaces "included" with using statment.

You need to choose which one you need and use it with fully qualified name (e.g. System.IO.File or Google.Apis.Drive.v2.Data.File depends on your needs).

If you wish to reduce keystrokes while writing code, you can do:

using GoogleDataAPI = Google.Apis.Drive.v2.Data;

And then use GoogleDataAPI instead of Google.Apis.Drive.v2.Data.

Upvotes: 3

LearningAsp
LearningAsp

Reputation: 361

don't give only File in definition ,go for the full namespace while defining

Upvotes: 0

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

I have a lot of class names that are identical, yet reside in different namespaces.

The other answers are correct; you can get around this by fully qualifying the class with its namespace. I prefer to use a using alias.

using System.IO;
using google = Google.Apis.Drive.v2.Data;

Any reference to System.IO.File can be left unqualified. Any time you need to reference Google's file class, you can simply use (for example):-

var googleFile = new google.File();

Upvotes: 0

BugFinder
BugFinder

Reputation: 17868

The best thing to do is then qualify the File type, if it will always been on a google drive, then set it to 'Google.Apis.Drive.v2.Data.File'

Upvotes: 0

Related Questions