Thanks to Bradley's answer, I managed a workaround: upload the file using FtpClient.OpenWrite (...), after which I constantly check the file size on the server, which solved the problem of code execution during the download process, which should be executed after it.
Then I came across another problem that I managed to work around: when checking the file size of the uploaded file on a remote FTP server, you cannot always get the file size via FtpClient.GetFileSize (...) to the FTP command (SIZE), which does not supported by all FTP servers. The solution was as follows: If FtpClient.GetFileSize (...) returns greater than -1, the SIZE command works and can be used. If the function returns -1, you can simply traverse the list of files in the specified folder on the server. There you get an FtpListItem array that has the Size property. Size returns the same value as FtpClient.GetFileSize (...) actually.
Code example:
const string folderPath = "/media/"; string fileName = "image.png"; string fullRemotePath = folderPath + fileName; string fileToUpload = @"C:\image.png"; FtpClient ftpClient = new FtpClient(); // start FtpClient connection etc FileInfo fileInfo = new FileInfo(fileToUpload); long actualFileSize = fileInfo.Length; using (var ftpStream = ftpClient.OpenWrite(fullRemotePath)) { using (var inputStream = new FileStream(fileToUpload, FileMode.Open)) { inputStream.CopyTo(ftpStream); //Thread.Sleep(5000); <-- not necessary anymore } } long fileSizeOnFtp = ftpClient.GetFileSize(fullRemotePath); while (fileSizeOnFtp < actualFileSize) { if (fileSizeOnFtp > -1) { fileSizeOnFtp = ftpClient.GetFileSize(fullRemotePath); } else { var ftpListItem = ftpClient.GetListing(folderPath).Where(x => x.Name == fileName).FirstOrDefault(); if (ftpListItem != null) { fileSizeOnFtp = ftpListItem.Size; } else { // the program only could run into this issue if the folder couldn't be listed throw new Exception(); } } } // execute everything else after ftp upload has finished
source share