Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Net;
  4. using System.Threading;
  5. using XML_Base.Config;
  6. using XML_Base_Library.Ftp;
  7.  
  8. namespace XML_Base
  9. {
  10.     class FtpUtil
  11.     {
  12.         private string username, password, serverip;
  13.         private int port, no = 1;
  14.         private Queue q = new Queue(11);
  15.         private Thread ftpThread;
  16.  
  17.         /// <summary>
  18.         /// Constructor of the FtpUtil thread.
  19.         /// It's used to initialise some internal variables.
  20.         /// </summary>
  21.         /// <param name="serverIp">The ftp server's ip address.</param>
  22.         /// <param name="Port">The ftp server's port.</param>
  23.         /// <param name="username">The ftp server's username used for access.</param>
  24.         /// <param name="password">The ftp server's password used for access.</param>
  25.         public FtpUtil(IPAddress serverIp, int Port, string username, string password)
  26.         {
  27.             try
  28.             {
  29.                 this.username = username;
  30.                 this.password = password;
  31.                 this.serverip = serverIp.ToString();
  32.                 this.port = Port;
  33.             }
  34.             catch (Exception ex)
  35.             {
  36.                 Util.ExceptionHandler.RaiseException(ex);
  37.             }
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Main method of the FtpUtil class. It downloads the first file in the queue.
  42.         /// </summary>
  43.         /// <remarks>Normally runs on a separate thread to avoid freezing MAINthread.</remarks>
  44.         private void GetFile()
  45.         {
  46.             try
  47.             {
  48.                 FormInstancer.MainFormInstance.SetStatus(String.Format("Downloading file {0} of {1}", no.ToString(), (q.Count + no).ToString()));
  49.                 FtpClient ftpClient = new FtpClient(serverip, port);
  50.                 //ftpClient.AutoChecksumValidation = HashingFunction.Crc32; Bulletproof Ftp Server 2010 returns "Unknown Command" on all types of hashes. We need to fix the autochecksumvalidation's server command to a working one.
  51.                 ftpClient.FileTransferType = TransferType.Binary;
  52.                 ftpClient.DataTransferMode = TransferMode.Passive;
  53.                 ftpClient.Open(username, password);
  54.                 string filename = (string)q.Dequeue();
  55.                 ftpClient.GetFile(Global.FtpRemotePath + filename, Global.FtpDownloadPath + filename, FileAction.Create);
  56.                 ftpClient.Close();
  57.                 no++;
  58.                 DownloadComplete();
  59.             }
  60.             catch
  61.             { FormInstancer.MainFormInstance.SetStatus("Error: Connection failed."); }
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Adds the FileName parameter to the queue and starts a new thread to download the file.
  66.         /// </summary>
  67.         /// <param name="FileName">File to be downloaded</param>
  68.         public void Download(string FileName)
  69.         {
  70.             try
  71.             {
  72.                 q.Enqueue(FileName);
  73.                 if (ftpThread == null)
  74.                 {
  75.                     ftpThread = new Thread(new ThreadStart(GetFile));
  76.                     ftpThread.Name = "Ftp thread";
  77.                     ftpThread.Start();
  78.                 }
  79.             }
  80.             catch (Exception ex)
  81.             {
  82.                 Util.ExceptionHandler.RaiseException(ex);
  83.             }
  84.         }
  85.  
  86.         /// <summary>
  87.         /// Adds the FileName array to the queue and starts a new thread to download the first file from the queue.
  88.         /// </summary>
  89.         /// <param name="FileNames">An array of strings indicating Filenames</param>
  90.         public void Download(string[] FileNames)
  91.         {
  92.             for (int i = 0; i < FileNames.Length - 1; i++)
  93.             {
  94.                 q.Enqueue(FileNames[i]);
  95.             }
  96.             ftpThread = new Thread(new ThreadStart(GetFile));
  97.             ftpThread.Start();
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Event used to continue downloading the other files in the queue.
  102.         /// </summary>
  103.         /// <remarks>Creates a new thread every time it's called.</remarks>
  104.         private void DownloadComplete()
  105.         {
  106.             if (q.Count > 0)
  107.             {
  108.                 ftpThread = new Thread(new ThreadStart(GetFile));
  109.                 ftpThread.Name = "Ftp Thread";
  110.                 ftpThread.Start();
  111.             }
  112.             else
  113.             {
  114.                 FormInstancer.MainFormInstance.SetStatus("Ready");
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement