/// <summary> /// Valida si existe el expediente el linea de un cliente /// </summary> /// <param name="sCliente">Número de cliente</param> /// <returns></returns> public bool ExisteExpediente(string sCliente) { string FullPath = string.Format("{0}/{1}/{2}/", FTPCredentials.Path, ConnectionString.FolderConnection, sCliente); if (FTPServer.DirectoryExists(string.Format("{0}/{1}/", FTPCredentials.Path, ConnectionString.FolderConnection), FTPCredentials.User, FTPCredentials.Password, sCliente) && FTPServer.CheckIfFileExistsOnServer(FullPath, FTPCredentials.User, FTPCredentials.Password, "Log.txt") && FTPServer.Count(string.Format("{0}/{1}/", FTPCredentials.Path, ConnectionString.FolderConnection), FTPCredentials.User, FTPCredentials.Password, sCliente).Count > 13) { return(true); } else { return(false); } }
/// <summary> /// Conecta al servidor FTP y descarga archivos /// </summary> /// <param name="FTPAddress"></param> /// <param name="filename"></param> /// <param name="username"></param> /// <param name="password"></param> private void downloadFile(string FTPAddress, string filename, string username, string password) { downloadedData = new byte[0]; try { string FullPath = string.Format("{0}/{1}/Records/", FTPCredentials.Path, ConnectionString.FolderConnection); if (bExiste = FTPServer.CheckIfFileExistsOnServer(FullPath, FTPCredentials.User, FTPCredentials.Password, string.Format("pcll-{0}.wav", Llamada.iIdLlamada))) { //else //Optional //lblStatus.Text = "Conectando..."; Application.DoEvents(); //Create FTP request //Note: format is ftp://server.com/file.ext FtpWebRequest request = (FtpWebRequest)WebRequest.Create(string.Format("ftp://{0}/{1}", FTPAddress, filename)); //Optional //lblStatus.Text = "Recibiendo Información..."; Application.DoEvents(); //Get the file size first (for progress bar) request.Method = WebRequestMethods.Ftp.GetFileSize; request.Credentials = new NetworkCredential(username, password); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = true; //don't close the connection int dataLength = (int)request.GetResponse().ContentLength; //Optional //lblStatus.Text = "Descargando Archivo..."; Application.DoEvents(); //Now get the actual data request = (FtpWebRequest)WebRequest.Create(string.Format("ftp://{0}/{1}", FTPAddress, filename)); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(username, password); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; //close the connection when done //Set up progress bar pgbDescarga.Value = 0; pgbDescarga.Maximum = dataLength; lbProgress.Text = "0 MB/" + BytesToMegabytes(dataLength).ToString("0.00") + "MB"; //Streams FtpWebResponse response = request.GetResponse() as FtpWebResponse; Stream reader = response.GetResponseStream(); //Download to memory //Note: adjust the streams here to download directly to the hard drive MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[1024]; //downloads in chuncks lbProgress.Visible = true; while (true) { Application.DoEvents(); //prevent application from crashing //Try to read the data int bytesRead = reader.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { //Nothing was read, finished downloading pgbDescarga.Value = pgbDescarga.Maximum; lbProgress.Text = string.Format("{0} MB/{0} MB", BytesToMegabytes(dataLength).ToString("0.00")); Application.DoEvents(); break; } else { //Write the downloaded data memStream.Write(buffer, 0, bytesRead); //Update the progress bar if (pgbDescarga.Value + bytesRead <= pgbDescarga.Maximum) { pgbDescarga.Value += bytesRead; lbProgress.Text = string.Format("{0} MB/{1} MB", BytesToMegabytes(pgbDescarga.Value).ToString("0.00"), BytesToMegabytes(dataLength).ToString("0.00")); pgbDescarga.Refresh(); Application.DoEvents(); } } } lbProgress.Visible = false; //Convert the downloaded stream to a byte array downloadedData = memStream.ToArray(); //Clean up reader.Close(); memStream.Close(); response.Close(); } } catch (Exception) { FlatMessageBox.Show("Error conectando al servidor FTP.", "OK", string.Empty, FlatMessageBoxIcon.Error); } //lblStatus.Text = "Descargando datos"; username = string.Empty; password = string.Empty; if (bExiste) { SaveDownload(filename); } }