viernes, 7 de abril de 2017

Pro C#: Módulo 09: Laboratorio 10: Código Fuente

Elementos de la Interfaz

        private System.Windows.Forms.TableLayoutPanel tlPrincipal;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtOrigen1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtDestino1;
        private System.Windows.Forms.Button btnArchivo1;
        private System.Windows.Forms.Button btnDescarga1;
        private System.Windows.Forms.ProgressBar pbTarea1;
        private System.Windows.Forms.StatusStrip stPrincipal;
        private System.Windows.Forms.ToolStripStatusLabel stTarea1;
        private System.Windows.Forms.ToolStripStatusLabel stTarea2;
        private System.Windows.Forms.ToolStripStatusLabel stTarea3;
        private System.Windows.Forms.Timer tmTarea1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.OpenFileDialog ofdArchivo;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txtOrigen2;
        private System.Windows.Forms.ProgressBar pbTarea2;
        private System.Windows.Forms.Button btnDescarga2;
        private System.Windows.Forms.TextBox txtDestino2;
        private System.Windows.Forms.Button btnArchivo2;

Código Fuente de Ejemplo:


public partial class frmFTP : Form
    {
        int Intervalo = 1;
        int CHUNK_SIZE = 512;

        public frmFTP()
        {
            InitializeComponent();
        }

        private void descargaArch(string Origen, string Destino)
        {
            //Request para el Servidor 
            FtpWebRequest peticion = (FtpWebRequest)WebRequest.Create(Origen);
            peticion.Method = WebRequestMethods.Ftp.DownloadFile;

            //Usuario Anónimo 
            peticion.Credentials = new NetworkCredential("anonymous", "dsantizo@galileo.edu");
            //Response creado a partir del Request
            FtpWebResponse respuesta = (FtpWebResponse)peticion.GetResponse();
            //Stream de Respuesta
            Stream respuestaStrm = respuesta.GetResponseStream();
            BinaryReader lector = new BinaryReader(respuestaStrm);

            //Stream de Escritura
            FileStream Archivo =
                new FileStream(Destino, FileMode.Create, FileAccess.Write);
            BinaryWriter escritor = new BinaryWriter(Archivo);
            //Define Buffer
            Byte[] Datos = new Byte[CHUNK_SIZE];
            int Posicion = 0;
            //Lee Datos
            Datos = lector.ReadBytes(CHUNK_SIZE);
            //Verifica que obtuvo información
            while(Datos.Length > 0)
            {
               
                escritor.Write(Datos);
              
                Posicion += Datos.Length;
                Datos = lector.ReadBytes(CHUNK_SIZE);
                Debug.WriteLine($"Posición: {Posicion:N0}");               
            }

            Archivo.Close();

            //TODO : Cambiar código para que escriba en un archivo destino


            Debug.WriteLine(
                $"Descarga Completa, Estado {respuesta.StatusDescription}");

            lector.Close();
            respuesta.Close();
        }

        private void frmFTP_Load(object sender, EventArgs e)
        {

        }


        private void btnArchivo1_Click(object sender, EventArgs e)
        {
            ofdArchivo.ShowDialog();
            txtDestino1.Text = ofdArchivo.FileName;
        }


        private void btnDescarga1_Click(object sender, EventArgs e)
        {
            tmTarea1.Enabled = true;
            Task tarea1 = new Task(() => descargaArch(txtOrigen1.Text, txtDestino1.Text));
            tarea1.Start();
            tmTarea1.Enabled = false;
            string sDestino = Path.GetFileName(txtDestino1.Text);
            stTarea1.Text = $"Tarea 1: {sDestino} Completo";
        }

        private void tmTarea1_Tick(object sender, EventArgs e)
        {
            if ((pbTarea1.Value + Intervalo) > pbTarea1.Maximum)
                pbTarea1.Value = pbTarea1.Minimum;
            else
                pbTarea1.Value += Intervalo;
        }

        private void btnDescarga2_Click(object sender, EventArgs e)
        {
            descargaArch(txtOrigen2.Text, txtDestino2.Text);
            string sDestino = Path.GetFileName(txtDestino1.Text);
            stTarea2.Text = $"Tarea 2: {sDestino} Completo";
        }

        private void btnArchivo2_Click(object sender, EventArgs e)
        {
            ofdArchivo.ShowDialog();
            txtDestino2.Text = ofdArchivo.FileName;
        }

    }

No hay comentarios.:

Publicar un comentario