Advertisement
Guest User

ftpClass

a guest
Jan 12th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace AR_Tool
  12. {
  13.     class ftpClient
  14.     {
  15.        
  16.        
  17.         Uri uri;
  18.         ProgressBar progressbar;
  19.         MyWebClient webclient;
  20.         string url;
  21.         string path;
  22.         string username;
  23.         string password;
  24.         NetworkCredential netcredentials;
  25.  
  26.         public ftpClient(string url, string path, string username, string password, ProgressBar progressbar)
  27.         {
  28.             this.url = url;
  29.             this.path = path;
  30.             this.username = username;
  31.             this.password = password;
  32.             this.progressbar = progressbar;
  33.             webclient = new MyWebClient();
  34.             netcredentials = new NetworkCredential(username, password);
  35.             webclient.Credentials = netcredentials;
  36.             uri = new Uri(url);
  37.             this.progressbar.Minimum = 0;
  38.             this.progressbar.Maximum = 100;
  39.             this.progressbar.Value = 0;
  40.            
  41.  
  42.         }
  43.  
  44.         public async void DownloadFile()
  45.         {
  46.             try
  47.             {
  48.                 webclient.DownloadProgressChanged += (s, t) =>
  49.                 {
  50.                    
  51.                     progressbar.Value = t.ProgressPercentage;
  52.                    
  53.                 };
  54.                 webclient.DownloadFileCompleted += (s, t) =>
  55.                 {
  56.                     progressbar.Visible = true;
  57.                     progressbar.Value = 0;
  58.  
  59.                 };
  60.                 await webclient.DownloadFileTaskAsync(uri, path);
  61.             }
  62.             catch (Exception c)
  63.             {
  64.                 MessageBox.Show("Connessione ad internet non disponibile, riprovare");
  65.             }
  66.         }
  67.  
  68.        
  69.  
  70.         internal class MyWebClient : WebClient
  71.         {
  72.             protected override WebRequest GetWebRequest(Uri address)
  73.             {
  74.                 FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
  75.                 req.UsePassive = false;
  76.                 return req;
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement