Пример #1
0
 /* Download File */
 public void Download(string localFile, string remoteFile)
 {
     try
     {
         /* Create an FTP Request */
         FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + remoteFile);
         /* Log in to the FTP Server with the User Name and Password Provided */
         FtpRequest.Credentials = new NetworkCredential(UserName, Password);
         LogInToInterlocutor(InterlocutorAddress);
         /* When in doubt, use these options */
         FtpRequest.UseBinary  = true;
         FtpRequest.UsePassive = true;
         FtpRequest.KeepAlive  = true;
         /* Specify the Type of FTP Request */
         FtpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
         /* Establish Return Communication with the FTP Server */
         FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
         /* Get the FTP Server's Response Stream */
         StartDownloadData(localFile, remoteFile, InterlocutorAddress);
         FtpStream = FtpResponse.GetResponseStream();
         /* Open a File Stream to Write the Downloaded File */
         FileStream localFileStream = new FileStream(localFile, FileMode.Create);
         /* Buffer for the Downloaded Data */
         byte[] byteBuffer = new byte[BufferSize];
         int    bytesRead  = FtpStream.Read(byteBuffer, 0, BufferSize);
         /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
         try
         {
             while (bytesRead > 0)
             {
                 localFileStream.Write(byteBuffer, 0, bytesRead);
                 bytesRead = FtpStream.Read(byteBuffer, 0, BufferSize);
             }
         }
         finally
         { /* Resource Cleanup */
             localFileStream.Close();
             EndDownloadData(localFile, remoteFile, InterlocutorAddress);
         }
     }
     finally
     {
         FtpStream.Close();
         FtpResponse.Close();
         FtpRequest = null;
     }
 }
Пример #2
0
        /* Get the Size of a File */
        public string GetFileSize(string fileName)
        {
            try
            {
                /* Create an FTP Request */
                FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + fileName);
                /* Log in to the FTP Server with the User Name and Password Provided */
                FtpRequest.Credentials = new NetworkCredential(UserName, Password);
                LogInToInterlocutor(InterlocutorAddress);
                /* When in doubt, use these options */
                FtpRequest.UseBinary  = true;
                FtpRequest.UsePassive = true;
                FtpRequest.KeepAlive  = true;
                /* Specify the Type of FTP Request */
                FtpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
                /* Establish Return Communication with the FTP Server */
                FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
                /* Establish Return Communication with the FTP Server */
                FtpStream = FtpResponse.GetResponseStream();
                /* Get the FTP Server's Response Stream */
                StreamReader ftpReader = new StreamReader(FtpStream);
                /* Store the Raw Response */
                string fileInfo = null;
                /* Read the Full Response Stream */
                try { while (ftpReader.Peek() != -1)
                      {
                          fileInfo = ftpReader.ReadToEnd();
                      }
                }
                finally
                {
                    /* Resource Cleanup */
                    ftpReader.Close();
                }

                /* Return File Size */
                return(fileInfo);
            }
            finally
            {
                FtpStream.Close();
                FtpResponse.Close();
                FtpRequest = null;
            }
        }
Пример #3
0
 /* List Directory Contents in Detail (Name, Size, Created, etc.) */
 public string[] DirectoryListDetailed(string directory)
 {
     try
     {
         /* Create an FTP Request */
         FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + directory);
         /* Log in to the FTP Server with the User Name and Password Provided */
         FtpRequest.Credentials = new NetworkCredential(UserName, Password);
         LogInToInterlocutor(InterlocutorAddress);
         /* When in doubt, use these options */
         FtpRequest.UseBinary  = true;
         FtpRequest.UsePassive = true;
         FtpRequest.KeepAlive  = true;
         /* Specify the Type of FTP Request */
         FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
         /* Establish Return Communication with the FTP Server */
         FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
         /* Establish Return Communication with the FTP Server */
         FtpStream = FtpResponse.GetResponseStream();
         /* Get the FTP Server's Response Stream */
         StreamReader ftpReader = new StreamReader(FtpStream);
         /* Store the Raw Response */
         string directoryRaw = null;
         /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
         try { while (ftpReader.Peek() != -1)
               {
                   directoryRaw += ftpReader.ReadLine() + "|";
               }
         }
         finally
         {  /* Resource Cleanup */
             ftpReader.Close();
         }
         /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
         return(directoryRaw.Split("|".ToCharArray()));
     }
     finally
     {
         FtpStream.Close();
         FtpResponse.Close();
         FtpRequest = null;
     }
 }
Пример #4
0
        /// <summary> Descargar un fichero al servidor Ftp. </summary>
        ///
        /// <param name="remoteFile"> Nombre del fichero remoto. </param>
        /// <param name="localFile"> Nombre del fichero local. </param>
        /// <param name="crearFichero"> Sobreescribir destino si ya existe. </param>
        ///
        /// <returns> Resultado (S/N/C) </returns>
        ///
        public Char FtpDownloadFile(string remoteFile, string localFile, int crearFichero = (int)FileMode.Create)
        {
            FileStream localFileStream;

            byte[] byteBuffer;
            int    bytesLeidos;

            Resultado = 'N';

            try
            {
                if (String.IsNullOrEmpty(FTPHost) || string.IsNullOrEmpty(FTPUser) || FTPPwd == null)
                {
                    UsrError  = "KO - Faltan parámetros de conexión al Host Ftp";
                    UsrErrorC = -1;
                    Resultado = 'N';

                    return(Resultado);
                }

                // Conexión con el Host Ftp.
                FtpRequest             = (FtpWebRequest)FtpWebRequest.Create(FTPHost + "/" + remoteFile);
                FtpRequest.Credentials = new NetworkCredential(FTPUser, FTPPwd);
                FtpRequest.Method      = WebRequestMethods.Ftp.DownloadFile;

                // Canal de comunicación con el Host Ftp.
                FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();

                // Retorno de comunicación con el Host Ftp.
                FtpFileStream = FtpResponse.GetResponseStream();

                // Canal de comunicación con el fichero local.
                localFileStream = new FileStream(localFile, FileMode.Create);
                byteBuffer      = new byte[_BYTE_BUFFER];

                // Leer datos del fichero remoto y grabar en fichero local.
                bytesLeidos = FtpFileStream.Read(byteBuffer, 0, _BYTE_BUFFER);

                while (bytesLeidos > 0)
                {
                    localFileStream.Write(byteBuffer, 0, _BYTE_BUFFER);
                    bytesLeidos = FtpFileStream.Read(byteBuffer, 0, _BYTE_BUFFER);
                }

                // Cerrar canales de comunicación Ftp.
                localFileStream.Close();
                FtpFileStream.Close();
                FtpRequest = null;
                FtpResponse.Close();

                Resultado = 'S';
                UsrError  = "OK";
            }

            catch (Exception ex)
            {
                // Error de Ftp.
                UsrErrorC = ex.HResult;
                UsrError  = ex.Message;
                UsrErrorE = ex.StackTrace;

                Resultado = 'C';
            }

            return(Resultado);
        }
Пример #5
0
        //------------------------------------------------------------------
        // Métodos públicos de la clase.
        //------------------------------------------------------------------

        /// <summary>
        ///
        /// </summary>
        /// <param name="remoteFile"> Nombre del fichero remoto. </param>
        ///
        /// <returns> Resultado (S/N/C) </returns>
        ///
        public Char FtpFileExists(string remoteFile)
        {
            // string fileInfo;                     Innecesario.

            Resultado = 'N';

            try
            {
                if (String.IsNullOrEmpty(FTPHost) || string.IsNullOrEmpty(FTPUser) || FTPPwd == null)
                {
                    UsrError  = "KO - Faltan parámetros de conexión al Host Ftp";
                    UsrErrorC = -1;
                    Resultado = 'N';

                    return(Resultado);
                }

                FtpRequest             = (FtpWebRequest)FtpWebRequest.Create(FTPHost + "/" + remoteFile);
                FtpRequest.Credentials = new NetworkCredential(FTPUser, FTPPwd);
                FtpRequest.Method      = WebRequestMethods.Ftp.GetDateTimestamp;

                // Canal de comunicación con el Host Ftp.
                FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();

                // Retorno de comunicación con el Host Ftp.
                FtpFileStream = FtpResponse.GetResponseStream();

                // Canal de lectura del Host Ftp. - Innecesario.
                // FtpStreamReader = new StreamReader(FtpFileStream);
                // fileInfo = FtpStreamReader.ReadToEnd();

                // Cerrar canales de comunicación Ftp.
                FtpFileStream.Close();
                FtpRequest = null;
                FtpResponse.Close();

                Resultado = 'S';
                UsrError  = "OK";
            }

            catch (WebException ex) when(ex.Status.ToString() == "ProtocolError")
            {
                // Error de Ftp.
                UsrErrorC = ex.HResult;
                UsrError  = ex.Message;
                UsrErrorE = ex.StackTrace;

                Resultado = 'N';
            }

            catch (Exception ex)
            {
                // Error de programa.
                UsrErrorC = ex.HResult;
                UsrError  = ex.Message;
                UsrErrorE = ex.StackTrace;

                Resultado = 'C';
            }

            return(Resultado);
        }