Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
{

}
}
Posted
Updated 20-Mar-19 10:52am
Comments
Mohibur Rashid 19-Mar-19 19:04pm    
Have you tried searching?
Abuamer 20-Mar-19 5:16am    
i have tried some little search but i am new in c# and i have a big problem with understanding the code. so please if you code help me i will be graceful.finally thank you at all
[no name] 19-Mar-19 22:55pm    
Why do you have an empty catch block? You think this is fun?
Abuamer 20-Mar-19 5:14am    
iam sorry for my weak syntax. i am new in c#.please forgive me . but i want you to know iam not jocking

1 solution

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!
 
Share this answer
 
Comments
Abuamer 21-Mar-19 10:31am    
really thank you so much my friend .i am really graceful
Member 13842259 22-Nov-21 1:47am    
https://github.com/Obrelix/.net-Google-Drive-API-v3-File-Handling

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900