C#错误CS0117-当我使用Google Drive API时,文件不包含ReadAllText的定义
我试图使一些客户端上传文件到谷歌驱动器。我从一些网站复制了部分代码,但仍然有一个错误。错误号为CS0117,表示该文件不包含ReadAllText的定义。错误在第55行。谢谢你的回答。这是我的密码:C#错误CS0117-当我使用Google Drive API时,文件不包含ReadAllText的定义,c#,C#,我试图使一些客户端上传文件到谷歌驱动器。我从一些网站复制了部分代码,但仍然有一个错误。错误号为CS0117,表示该文件不包含ReadAllText的定义。错误在第55行。谢谢你的回答。这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using File = Google.Apis.Drive.v3.Data.File;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace IDK_client
{
public partial class Form1 : Form
{
//Upload variables
public bool uploadFilesSelected = false;
public string uploadFileContent;
//Google Drive connection variables
static string[] Scopes = { DriveService.Scope.Drive };
static string ApplicationName = "server";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
labelUploadFile.Visible = false;
}
private void buttonUpload_Click(object sender, EventArgs e)
{
if(uploadFilesSelected == false)
{
try
{
OpenFileDialog openFileDialogUpload = new OpenFileDialog();
openFileDialogUpload.CheckFileExists = true;
openFileDialogUpload.CheckPathExists = true;
openFileDialogUpload.InitialDirectory = @"C:\Users\";
openFileDialogUpload.RestoreDirectory = true;
openFileDialogUpload.Title = "Select file to upload...";
openFileDialogUpload.Filter = "All files (*.*)|*.*";
openFileDialogUpload.ShowDialog();
labelUploadFile.Text = openFileDialogUpload.SafeFileName;
uploadFileContent = File.ReadAllText(openFileDialogUpload.FileName);
labelUploadFile.Visible = true;
uploadFilesSelected = true;
}
catch
{
MessageBox.Show("Can not open file.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
}
else if(uploadFilesSelected == false)
{
UserCredential credential;
credential = GetCredentials();
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
//uploadFile();
}
}
public void uploadFile(string path, DriveService service)
{
var fileMetadata = new Google.Apis.Drive.v3.Data.File();
fileMetadata.Name = Path.GetFileName(path);
fileMetadata.MimeType = "txt";
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
{
request = service.Files.Create(fileMetadata, stream, "txt");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
}
private static UserCredential GetCredentials()
{
UserCredential credential;
using (var stream = new FileStream("client_id.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result;
// Console.WriteLine("Credential file saved to: " + credPath);
}
return credential;
}
}
}
很可能您还有其他名为
文件的文件
。用System.IO.File.ReadAllText
完全限定它,我猜您可能有一个冲突的类。请尝试System.IO.File.ReadAllText(…
Right.OP正在使用File=Google.api.Drive.v3.Data.File;