Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Net;
- using System.Threading;
- using XML_Base.Config;
- using XML_Base_Library.Ftp;
- namespace XML_Base
- {
- class FtpUtil
- {
- private string username, password, serverip;
- private int port, no = 1;
- private Queue q = new Queue(11);
- private Thread ftpThread;
- /// <summary>
- /// Constructor of the FtpUtil thread.
- /// It's used to initialise some internal variables.
- /// </summary>
- /// <param name="serverIp">The ftp server's ip address.</param>
- /// <param name="Port">The ftp server's port.</param>
- /// <param name="username">The ftp server's username used for access.</param>
- /// <param name="password">The ftp server's password used for access.</param>
- public FtpUtil(IPAddress serverIp, int Port, string username, string password)
- {
- try
- {
- this.username = username;
- this.password = password;
- this.serverip = serverIp.ToString();
- this.port = Port;
- }
- catch (Exception ex)
- {
- Util.ExceptionHandler.RaiseException(ex);
- }
- }
- /// <summary>
- /// Main method of the FtpUtil class. It downloads the first file in the queue.
- /// </summary>
- /// <remarks>Normally runs on a separate thread to avoid freezing MAINthread.</remarks>
- private void GetFile()
- {
- try
- {
- FormInstancer.MainFormInstance.SetStatus(String.Format("Downloading file {0} of {1}", no.ToString(), (q.Count + no).ToString()));
- FtpClient ftpClient = new FtpClient(serverip, port);
- //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.
- ftpClient.FileTransferType = TransferType.Binary;
- ftpClient.DataTransferMode = TransferMode.Passive;
- ftpClient.Open(username, password);
- string filename = (string)q.Dequeue();
- ftpClient.GetFile(Global.FtpRemotePath + filename, Global.FtpDownloadPath + filename, FileAction.Create);
- ftpClient.Close();
- no++;
- DownloadComplete();
- }
- catch
- { FormInstancer.MainFormInstance.SetStatus("Error: Connection failed."); }
- }
- /// <summary>
- /// Adds the FileName parameter to the queue and starts a new thread to download the file.
- /// </summary>
- /// <param name="FileName">File to be downloaded</param>
- public void Download(string FileName)
- {
- try
- {
- q.Enqueue(FileName);
- if (ftpThread == null)
- {
- ftpThread = new Thread(new ThreadStart(GetFile));
- ftpThread.Name = "Ftp thread";
- ftpThread.Start();
- }
- }
- catch (Exception ex)
- {
- Util.ExceptionHandler.RaiseException(ex);
- }
- }
- /// <summary>
- /// Adds the FileName array to the queue and starts a new thread to download the first file from the queue.
- /// </summary>
- /// <param name="FileNames">An array of strings indicating Filenames</param>
- public void Download(string[] FileNames)
- {
- for (int i = 0; i < FileNames.Length - 1; i++)
- {
- q.Enqueue(FileNames[i]);
- }
- ftpThread = new Thread(new ThreadStart(GetFile));
- ftpThread.Start();
- }
- /// <summary>
- /// Event used to continue downloading the other files in the queue.
- /// </summary>
- /// <remarks>Creates a new thread every time it's called.</remarks>
- private void DownloadComplete()
- {
- if (q.Count > 0)
- {
- ftpThread = new Thread(new ThreadStart(GetFile));
- ftpThread.Name = "Ftp Thread";
- ftpThread.Start();
- }
- else
- {
- FormInstancer.MainFormInstance.SetStatus("Ready");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement