如何使用桌面应用程序C#将文件上传到googledrive [英] How to upload afile into googledrive using desktop application C#

查看:79
本文介绍了如何使用桌面应用程序C#将文件上传到googledrive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个桌面应用程序,我想用它将文件上传到我的google drive.i创建了一个上传文件的方法>我按下按钮点击方法。

但问题是代码不起作用。

可以任何人帮助



我尝试了什么:



private void btnUpload_Click(object sender,EventArgs e)

{

上传(@c:\ Serial.Text); //这里我把文件上传到

}

public void Upload(string FileToUpload)

{

try

{

FileInfo ToUpload = new FileInfo (FileToUpload);

FtpWebRequest request =(FtpWebRequest)WebRequest.Create(https://drive.google.com/drive/my-drive/+ ToUpload.Name);

request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = new NetworkCredential(UserName,Password);

Stream ftpstream = request .GetRequestStream();

FileStream ft = File.OpenRead(FileToUpload); int length = 1024;

byte [] buffer = new byte [length];

int byteread = 0;

do

{

byteread = ft.Read(buffer,0,length);

ftpstream.Write(buffer,0,byteread);

}

while(byteread!= 0);

ft.Close();

ftpstream.Close();

}

catch

{



}

}

解决方案

坏消息我的朋友,Google云端硬盘不支持FTP作为上传文件的方式。他们也不会像您一样直接在Web请求中接受凭据。它们仅支持OAuth连接。有许多与安全相关的原因。


我很久以前写了一篇文章(在WPF中使用Google Drive [ ^ ])覆盖了您尝试执行的方案。但是...... Google已经弃用了该API,因此您无法使用文章中的代码,但身份验证工作流程基本相同,这将有助于您了解所需的步骤。



当我写这篇文章时,Google没有.Net的API客户端库。那已经改变了。您可以下载客户端库并在适用于.NET的Drive API客户端库中查看示例  | 适用于.NET的API客户端库  |  Google Developers [ ^ ]该库应该让您在应用程序中直接支持Drive。它甚至可以直接从Visual Studio作为NuGet包提供:在NuGet上 Google.Apis.Drive.v3。组织 [ ^ ]



但是,我会说,对于C#的新手来说,这可能是一项艰巨的任务。如果您要查找信息,请确保它适用于最新的Google API。 Drive已经存在了很长时间,Google已经至少两次更改了API。 (我的文章是针对他们的驱动API的v2写的。)由于谷歌和其他网站永远不会忘记,你可能会发现很多过时的信息。



好运气!

i have a desktop application which i want to use to upload Files into my google drive.i created a Method To Upload the file > i call the method in the button click.
but the problem is the code doesn't work.
can any one help

What I have tried:

private void btnUpload_Click(object sender, EventArgs e)
{
Upload(@"c:\Serial.Text");// this where i put the file to upload it
}
public void Upload(string FileToUpload)
{
try
{
FileInfo ToUpload = new FileInfo(FileToUpload);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("https://drive.google.com/drive/my-drive/"+ToUpload.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("UserName", "Password");
Stream ftpstream = request.GetRequestStream();
FileStream ft = File.OpenRead(FileToUpload); int length = 1024;
byte[] buffer = new byte[length];
int byteread = 0;
do
{
byteread = ft.Read(buffer, 0, length);
ftpstream.Write(buffer, 0, byteread);
}
while (byteread != 0);
ft.Close();
ftpstream.Close();
}
catch
{

}
}

解决方案

Bad news my friend, Google Drive doesn't support FTP as a means of uploading a file. They also don't accept credentials directly in the web request as you have done. They only support OAuth connections. There is a number of security related reasons for this.

I wrote an article a long time ago (Working with Google Drive in WPF[^]) that covered the scenario you are trying to do. However... Google has since deprecated that API so you can't use the code in the article but the authentication workflow is essentially the same which will help you understand the steps required.

At the time I wrote that, there was no API client library for .Net from Google. That has changed. You can download the client library and view a sample at Drive API Client Library for .NET  |  API Client Library for .NET  |  Google Developers[^] That library should let you just drop-in support for Drive in your application. It is even available as a NuGet package directly from Visual Studio: Google.Apis.Drive.v3 on NuGet.org[^]

I'll say however, that this might be a daunting task for someone new to C#. If you go looking for information, make sure it applies to the latest Google APIs. Drive has been around a long time and Google has changed the APIs at least twice before. (My article was written against v2 of their drive API.) Since Google and other web sites never forget, you might find a lot of out-dated information.

Good luck!


这篇关于如何使用桌面应用程序C#将文件上传到googledrive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆