0

As I can upload all .txt files from one folder to an FTP folder. I can upload only one file but I need to upload all the files that are inside a folder on your computer to an FTP folder

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://123.456.789.00/folder1/folder2" + "/" + Path.GetFileName("D:\\folderUpload\\1test.txt"));

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential("username", "pass");
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            FileStream stream = File.OpenRead("D:\\folderUpload\\1test.txt");
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Close();

            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();

            MessageBox.Show("Upload OK");
wabregoc
  • 1,064
  • 2
  • 12
  • 17
  • 2
    Refactor your code and create a method which accepts a file and uploads it to ftp server. Then for each file in your source folder, call that method and pass the file. – Reza Aghaei Mar 08 '16 at 20:30
  • Possible duplicate of [Upload file to ftp using c#](http://stackoverflow.com/questions/15268760/upload-file-to-ftp-using-c-sharp) – Brandon Mar 08 '16 at 20:32

2 Answers2

0

Tou can do something like that (it can be improved if you want to keep the original tree or parallelize...):

static public void Main(string[] args)
    {
       DirectoryInfo directory = DirectoryInfo(@"C:\PathToUpload");
       foreach(var file in directory.GetFiles(*)){
           UploadFile(file, "ftp://123.456.789.00/folder1/folder2");
       }

   MessageBox.Show("Upload OK");

}


public void UploadFile(FileInfo file,string ftpUrl){    
    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpUrl + "/" + file.Name);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential("username", "pass");
    request.UsePassive = true;
    request.UseBinary = true;
    request.KeepAlive = false;
    FileStream stream = File.OpenRead("D:\\folderUpload\\1test.txt");
    byte[] buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);
    stream.Close();
    Stream reqStream = request.GetRequestStream();
    reqStream.Write(buffer, 0, buffer.Length);
    reqStream.Close();
}
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
-1
public static void uploadFolder(string source, string uploadpath)
{
    WebRequest request = WebRequest.Create(uploadpath); 
    request.Credentials = new NetworkCredential(userName, password); 
    string[] files = Directory.GetFiles(source, "*.*"); 
    string[] subFolders = Directory.GetDirectories(source); 
    foreach(string file in files) 
    { 
        request = WebRequest.Create(file);
        request.Method = WebRequestMethods.Ftp.UploadFile; 
    }
    foreach(string subFolder in subFolders) 
    { 
        request = WebRequest.Create(uploadpath + "/"+ Path.GetFileName(subFolder)); 
        request.Method = WebRequestMethods.Ftp.MakeDirectory; <br/>
        request.Credentials = new NetworkCredential(userName,password); 
        uploadFolder(subFolder, uploadpath+"/"+Path.GetFileName(subFolder));
    }
}
Alex
  • 4,885
  • 3
  • 19
  • 39
  • We usually ask that you provide some context to your answer by explaining how it works. https://stackoverflow.com/help/answering – TidyDev Nov 09 '17 at 08:36
  • Please note that you should not use
    tags to format your code. Use "Editing" menu to format text instead.
    – Alex Nov 09 '17 at 08:37