0

以下のコードを使用してGoogleドライブアカウントにファイルを挿入しているときにエラーが発生しました

「ファイル」は、「Google.Apis.Drive.v2.Data.File」と「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;
            }
        }
4

5 に答える 5

3

両方の名前空間 (Google.Apis.Drive.v2.DataおよびSystem.IO) が定義されてclass Fileおり、おそらく、両方の名前空間がステートメントに「含まれています」using

必要なものを選択し、完全修飾名で使用する必要があります (たとえばSystem.IO.File、またはGoogle.Apis.Drive.v2.Data.File必要に応じて異なります)。

コードを書くときにキーストロークを減らしたい場合は、次のようにできます。

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

そして、GoogleDataAPIの代わりに使用しGoogle.Apis.Drive.v2.Dataます。

于 2012-10-19T09:51:03.677 に答える
2

Google.Apis.Drive.v2.DataどちらもFileクラスをSystem.IO定義しているため、フルネーム とGoogle.Apis.Drive.v2.Data.FileSystem.IO.File

于 2012-10-19T09:56:26.017 に答える
0

最善の方法は、ファイルの種類を修飾することです。ファイルが常に Google ドライブにある場合は、「Google.Apis.Drive.v2.Data.File」に設定します。

于 2012-10-19T09:51:13.573 に答える
0

同一であるにもかかわらず、異なる名前空間に存在するクラス名がたくさんあります。

他の答えは正しいです。クラスを名前空間で完全に修飾することで、これを回避できます。私は using エイリアスを使用することを好みます。

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

System.IO.File への参照は、修飾しないままにすることができます。Google のファイル クラスを参照する必要がある場合はいつでも、次のように簡単に使用できます (たとえば):-

var googleFile = new google.File();
于 2012-10-19T09:54:45.670 に答える
0

定義でファイルのみを指定しないでください。定義中に完全な名前空間を使用してください

于 2012-10-19T09:56:09.093 に答える