Uploading a file directory using WebClient

I searched and searched and cannot find a way to do this. I have files in the directory that I want to download. File names change constantly, so I can’t upload by file name. Here is what I have tried.

using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential("User", "Password"); foreach (var filePath in files) client.UploadFile("ftp://site.net//PICS_CAM1//", "STOR", @"PICS_CAM1\"); } 

But I get a compiler error:

The name "files" does not exist in the current context

Everything I researched says this should work.

Does anyone have a good way to upload a file directory via WebClient ?

+1
c # ftp webclient
May 16 '17 at 22:30
source share
2 answers

You must define and install files . If you want to upload all the files to a specific local directory, use, for example, Directory.EnumerateFiles .

Also, the address argument of WebClient.UploadFile should be the full URL of the target file, not just the URL of the target directory.

 IEnumerable<string> files = Directory.EnumerateFiles(@"C:\local\folder"); using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential("username", "password"); foreach (string file in files) { client.UploadFile( "ftp://example.com/remote/folder/" + Path.GetFileName(file), file); } } 
0
May 17 '17 at 6:00
source share

I think your web client download will work fine. Your problem is that your files variable is missing in scope.

You need to place more of your code so we can see better

0
May 17 '17 at 4:47
source share



All Articles