Mostrando las entradas con la etiqueta Pro C#. Mostrar todas las entradas
Mostrando las entradas con la etiqueta Pro C#. Mostrar todas las entradas

jueves, 20 de abril de 2017

Pro C#: Módulo 03: Código Fuente: Estructura EmpleadoEstatico


Código Fuente de Ejemplo

EmpleadoEstatico.cs


    /// <summary>
    /// Estructura para Empleados
    /// </summary>
    public struct EmpleadoEstatico
    {
        private string Temporal; //Campo Privado
        //Propiedad Automática
        public string Apellido { get; set; }

        private string mNombre;

        //Propiedad Codificada
        /// <summary>
        /// Propiedad Nombre
        /// </summary>
        public string Nombre
        {
            get { return mNombre; }
            set { mNombre = value; }
        }

        public DateTime FechaNacimiento { get; set; }

       
        private int mEdad2;

        public int Edad2
        {
            get
            {
                mEdad2 = (int)
                    (Now.Subtract(FechaNacimiento).TotalDays / 365.25);
                return mEdad2;
            }          
        }

        /// <summary>
        /// Método que devuelva la Edad del
        /// Empleado
        /// </summary>
        /// <returns>int positivo</returns>
        public int Edad()
        {
            int mEdad = 0;
            mEdad = (int)
                (Now.Subtract(FechaNacimiento).TotalDays / 365.25f);
            return mEdad;
        }

        /// <summary>
        /// Inicializa la estructura y asigna el Apellido
        /// </summary>
        /// <param name="Apellido"></param>
        public EmpleadoEstatico(string Apellido)
        {
            Temporal = "";
            mEdad2 = 0;
            FechaNacimiento = Now;
            this.Apellido = Apellido;
            mNombre = "N/D";
            //Nombre = "N/D";
        }

        /// <summary>
        /// Inicializa la estructura
        /// Asigna Apellido y Nombre
        /// </summary>
        /// <param name="Apellido"></param>
        /// <param name="Nombre"></param>
        public EmpleadoEstatico(string Apellido,
            string Nombre)
        {
            Temporal = "";
            mEdad2 = 0;
            FechaNacimiento = Now;
            this.Apellido = Apellido;
            mNombre = Nombre;
        }

    }



Program.cs


    class Program
    {
        static void Main(string[] args)

        {
            EmpleadoEstatico Emp1;
            Emp1 = new EmpleadoEstatico();
            Emp1.Apellido = "Ape 1";
            Emp1.FechaNacimiento = new DateTime(1990, 4, 20);
            Console.WriteLine(
                $"Método: {Emp1.Edad()}");
            Console.WriteLine(
                $"Propiedad: {Emp1.Edad2}");

            EmpleadoEstatico Emp2, Emp3;
            Emp2 = new EmpleadoEstatico("Ape 2");
            Emp3 = new EmpleadoEstatico(
                Nombre: "Nombre 3", Apellido: "Ape 3");
           
        }

    }


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;
        }

    }