BradleyUffner

MultiFileAsyncFtpDownload

Jul 6th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. public async Task DownloadMultipleFilesConcurrentFromFTP(int NumberOfConnections, string AppName, string RemoteUrl)
  2. {
  3.     var downloadTasks = new List<Task>();
  4.     for (int i = 0; i < NumberOfConnections; i++){
  5.         downloadTasks.Add(DownloadFile(AppName, RemoteUrl));
  6.     }
  7.     await Task.WhenAll(downloadTasks);
  8. }
  9.  
  10. public async Task DownloadFile(string AppName, string RemoteUrl)
  11. {
  12.     var ftp = new FtpClient(FTPHost, networkCredential);
  13.     ftp.ConnectTimeout = ConnectTimeout;
  14.     ftp.SocketPollInterval = SocketPollInterval;
  15.     ftp.ReadTimeout = ReadTimeout;
  16.     ftp.DataConnectionConnectTimeout = DataConnectionConnectTimeout;
  17.     ftp.DataConnectionReadTimeout = DataConnectionReadTimeout;
  18.     await ftp.ConnectAsync();
  19.     if (ftp.IsConnected)
  20.     {
  21.         using (FileStream read = new FileStream("settings.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  22.         {
  23.             if (ftp.IsConnected == true)
  24.             {
  25.                 if (File.Exists("settings.xml"))
  26.                 {
  27.                     Information info = (Information)xs.Deserialize(read);
  28.                     if (Directory.Exists(info.Downloads))
  29.                     {
  30.                         await ftp.DownloadFileAsync(info.Downloads + "\\" + AppName, RemoteUrl, FtpLocalExists.Overwrite, FtpVerify.Retry);
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }
Add Comment
Please, Sign In to add comment