SetupDownload.cs
1 using sage.ew.functions;
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Net;
7 using System.Text;
8 
9 namespace sage._50.Clases
10 {
14  public class SetupDownload
15  {
16  #region Propiedades
17  public string _MsgError = string.Empty;
21 
25  public string _FileLocal = string.Empty;
26  #endregion Propiedades
27 
28  #region Métodos públicos
29 
30 
36  public bool _DescargarArchivoFTP(DatosFTP datosFtp)
37  {
38  bool llOk = true;
39  _FileLocal = string.Empty;
40 
41  //Obtener la Uri del servidor
42  string lcRemoteUri = datosFtp.ToRemoteUri();
43 
44  string lcDirTemp = datosFtp._CarpetaLocal;
45  if (!Directory.Exists(lcDirTemp))
46  FUNCTIONS._CrearCarpetaTemp(lcDirTemp);
47  string lcFileName = Path.Combine(lcDirTemp, datosFtp._Archivo);
48 
49  string lcFileFtp = null;
50 
51  try
52  {
53  // Ruta fichero en el FTP
54  lcFileFtp = lcRemoteUri + "//" + datosFtp._Carpeta + (string.IsNullOrWhiteSpace(datosFtp._Carpeta) ? "" : "//") + datosFtp._Archivo;
55 
56  FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(lcFileFtp);
57  if (!string.IsNullOrWhiteSpace(datosFtp._Proxy))
58  {
59  WebProxy proxyObject = new WebProxy(datosFtp._Proxy, true);
60  ftpRequest.Proxy = proxyObject;
61  }
62  ftpRequest.UsePassive = !datosFtp._Ftp_activo;
63  ftpRequest.EnableSsl = datosFtp._Secure;
64  if (datosFtp._Secure && !datosFtp._ValidateServerCert)
65  {
66  ServicePointManager.ServerCertificateValidationCallback =
67  (sender, certificate, chain, errors) =>
68  {
69  return true;
70  };
71  }
72 
73  ftpRequest.Timeout = 10000;
74  ftpRequest.Credentials = new NetworkCredential(datosFtp._User, datosFtp._Password);
75  ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
76 
77  // bajar fichero
78  using (Stream ftpStream = ftpRequest.GetResponse().GetResponseStream())
79  {
80  using (Stream fileStream = File.Create(lcFileName))
81  {
82  ftpStream.CopyTo(fileStream);
83  }
84  }
85 
86  }
87  catch (Exception loEx)
88  {
89  llOk = false;
90  _MsgError = loEx.Message;
91  lcFileName = "";
92  }
93  finally
94  {
95  ServicePointManager.ServerCertificateValidationCallback = null;
96  }
97 
98  if (llOk)
99  _FileLocal = lcFileName;
100 
101 
102  return llOk && File.Exists(lcFileName);
103  }
104 
105  #endregion Métodos públicos
106 
107  #region Clase DatosFP
108 
109 
113  public class DatosFTP
114  {
118  public string _Server
119  {
120  get { return _cServer; }
121  set { _cServer = value; }
122  }
123  private string _cServer = "";
124 
128  public string _User
129  {
130  get { return _cUser; }
131  set { _cUser = value; }
132  }
133  private string _cUser = "";
134 
135 
139  public string _Password
140  {
141  get { return _cPassword; }
142  set { _cPassword = value; }
143  }
144  private string _cPassword = "";
145 
146 
150  public string _Carpeta
151  {
152  get { return _cCarpeta; }
153  set { _cCarpeta = value; }
154  }
155  private string _cCarpeta = "";
156 
157 
161  public string _Archivo
162  {
163  get { return _cArchivo; }
164  set { _cArchivo = value; }
165  }
166  private string _cArchivo = "";
167 
171  public string _CarpetaLocal
172  {
173  get { return _cCarpetaLocal; }
174  set { _cCarpetaLocal = value; }
175  }
176  private string _cCarpetaLocal = "";
177 
181  public int _Puerto
182  {
183  get { return _nPuerto; }
184  set { _nPuerto = value; }
185  }
186  private int _nPuerto = 21;
187 
191  public string _Proxy
192  {
193  get { return _cProxy; }
194  set { _cProxy = value; }
195  }
196  private string _cProxy = "";
197 
201  public bool _Secure
202  {
203  get { return _lSecure; }
204  set { _lSecure = value; }
205  }
206  private bool _lSecure = false;
207 
211  public bool _ValidateServerCert
212  {
213  get { return _lValidateServerCert; }
214  set { _lValidateServerCert = value; }
215  }
216  private bool _lValidateServerCert = false;
217 
221  public bool _Ftp_activo
222  {
223  get { return _lFtp_activo; }
224  set { _lFtp_activo = value; }
225  }
226  private bool _lFtp_activo = false;
227 
228 
229  #region Constructor
230  public DatosFTP()
234  {
235  }
236  #endregion Constructor
237 
242  public string ToRemoteUri()
243  {
244  string lcRemoteUri = $"{_Server}:{_Puerto}";
245 
246  if (!lcRemoteUri.ToLower().StartsWith("ftp:") && !lcRemoteUri.ToLower().StartsWith("http"))
247  {
248  lcRemoteUri = "ftp://" + lcRemoteUri;
249  }
250 
251  return lcRemoteUri;
252  }
253  }
254 
255  #endregion Clase DatosFP
256  }
257 
258 
259 }
Clase con los datos para la descarga
bool _ValidateServerCert
validar certificado servidor
bool _DescargarArchivoFTP(DatosFTP datosFtp)
Método para descargar un archivo
Clase para la descarga de ficheros FTP
string ToRemoteUri()
Devuelve la Uri del servidor