Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Net;
- using System.Net.FtpClient;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace InfiniteConnect
- {
- class FTPConnection
- {
- /// <summary>
- /// Private class objects.
- /// </summary>
- private string ftpHost;
- private string ftpUser;
- private string ftpPass;
- private int ftpPort;
- private int ftpTimeout;
- /// <summary>
- /// Class constructor.
- /// </summary>
- /// <param name="ftpHost"></param>
- /// <param name="ftpPort"></param>
- /// <param name="ftpUser"></param>
- /// <param name="ftpPass"></param>
- /// <param name="ftpDocRoot"></param>
- /// <param name="ftpTimeout"></pparam>
- public FTPConnection(string ftpHost, int ftpPort, string ftpUser, string ftpPass, int ftpTimeout = 30)
- {
- this.ftpHost = ftpHost;
- this.ftpPort = ftpPort;
- this.ftpUser = ftpUser;
- this.ftpPass = ftpPass;
- this.ftpTimeout = ftpTimeout;
- }
- /// <summary>
- /// Method to download stock file from ftp server.
- /// </summary>
- /// <param name="stockFileDir"></param>
- /// <param name="supplierId"></param>
- /// <param name="stockFileNamePattern"></param>
- /// <returns></returns>
- public string DownloadStockFile(string stockFileDir, int supplierId, string stockFileNamePattern)
- {
- // Init
- string localFilePath = "";
- // Load remote ftp server file
- using (FtpClient conn = new FtpClient())
- {
- // Set connection details
- conn.Encoding = Encoding.UTF8;
- conn.Host = ftpHost;
- conn.Port = ftpPort;
- conn.ConnectTimeout = 1000 * ftpTimeout;
- conn.Credentials = new NetworkCredential(ftpUser, ftpPass);
- // Get file listng
- foreach (FtpListItem ftpListItem in conn.GetListing(stockFileDir, FtpListOption.Modify | FtpListOption.Size))
- {
- // Proceed if this is a file
- if (ftpListItem.Type == FtpFileSystemObjectType.File && ftpListItem.Name.Contains(stockFileNamePattern))
- {
- // Download file
- localFilePath = string.Format(@"{0}\{1}_{2}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), supplierId, ftpListItem.Name);
- if (File.Exists(localFilePath)) File.Delete(localFilePath);
- using (var ftpStream = conn.OpenRead(ftpListItem.FullName))
- using (var fileStream = File.Create(localFilePath, (int)ftpStream.Length))
- {
- var buffer = new byte[8 * 1024];
- int count;
- while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
- {
- fileStream.Write(buffer, 0, count);
- }
- // Stop further processing
- break;
- }
- }
- }
- }
- // Finished
- return localFilePath;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement