Advertisement
Guest User

Latheesan

a guest
Feb 26th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.FtpClient;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace InfiniteConnect
  10. {
  11.     class FTPConnection
  12.     {
  13.         /// <summary>
  14.         /// Private class objects.
  15.         /// </summary>
  16.         private string ftpHost;
  17.         private string ftpUser;
  18.         private string ftpPass;
  19.         private int ftpPort;
  20.         private int ftpTimeout;
  21.  
  22.         /// <summary>
  23.         /// Class constructor.
  24.         /// </summary>
  25.         /// <param name="ftpHost"></param>
  26.         /// <param name="ftpPort"></param>
  27.         /// <param name="ftpUser"></param>
  28.         /// <param name="ftpPass"></param>
  29.         /// <param name="ftpDocRoot"></param>
  30.         /// <param name="ftpTimeout"></pparam>
  31.         public FTPConnection(string ftpHost, int ftpPort, string ftpUser, string ftpPass, int ftpTimeout = 30)
  32.         {
  33.             this.ftpHost = ftpHost;
  34.             this.ftpPort = ftpPort;
  35.             this.ftpUser = ftpUser;
  36.             this.ftpPass = ftpPass;
  37.             this.ftpTimeout = ftpTimeout;
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Method to download stock file from ftp server.
  42.         /// </summary>
  43.         /// <param name="stockFileDir"></param>
  44.         /// <param name="supplierId"></param>
  45.         /// <param name="stockFileNamePattern"></param>
  46.         /// <returns></returns>
  47.         public string DownloadStockFile(string stockFileDir, int supplierId, string stockFileNamePattern)
  48.         {
  49.             // Init
  50.             string localFilePath = "";
  51.  
  52.             // Load remote ftp server file
  53.             using (FtpClient conn = new FtpClient())
  54.             {
  55.                 // Set connection details
  56.                 conn.Encoding = Encoding.UTF8;
  57.                 conn.Host = ftpHost;
  58.                 conn.Port = ftpPort;
  59.                 conn.ConnectTimeout = 1000 * ftpTimeout;
  60.                 conn.Credentials = new NetworkCredential(ftpUser, ftpPass);
  61.  
  62.                 // Get file listng
  63.                 foreach (FtpListItem ftpListItem in conn.GetListing(stockFileDir, FtpListOption.Modify | FtpListOption.Size))
  64.                 {
  65.                     // Proceed if this is a file
  66.                     if (ftpListItem.Type == FtpFileSystemObjectType.File && ftpListItem.Name.Contains(stockFileNamePattern))
  67.                     {
  68.                         // Download file
  69.                         localFilePath = string.Format(@"{0}\{1}_{2}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), supplierId, ftpListItem.Name);
  70.                         if (File.Exists(localFilePath)) File.Delete(localFilePath);
  71.                         using (var ftpStream = conn.OpenRead(ftpListItem.FullName))
  72.                         using (var fileStream = File.Create(localFilePath, (int)ftpStream.Length))
  73.                         {
  74.                             var buffer = new byte[8 * 1024];
  75.                             int count;
  76.                             while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
  77.                             {
  78.                                 fileStream.Write(buffer, 0, count);
  79.                             }
  80.  
  81.                             // Stop further processing
  82.                             break;
  83.                         }
  84.                     }
  85.                 }
  86.             }
  87.  
  88.             // Finished
  89.             return localFilePath;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement