private void DownloadFileWithProgress(string DownloadLink, string TargetPath, Guna.UI2.WinForms.Guna2ProgressBar progress, Label labelProgress) { //Start Download // Function will return the number of bytes processed // to the caller. Initialize to 0 here. int bytesProcessed = 0; // Assign values to these objects here so that they can // be referenced in the finally block Stream remoteStream = null; Stream localStream = null; WebResponse response = null; // Use a try/catch/finally block as both the WebRequest and Stream // classes throw exceptions upon error try { // Create a request for the specified remote file name WebRequest request = WebRequest.Create(DownloadLink); if (request != null) { // Send the request to the server and retrieve the // WebResponse object // Get the Full size of the File double TotalBytesToReceive = 0; var SizewebRequest = HttpWebRequest.Create(new Uri(DownloadLink)); SizewebRequest.Method = "HEAD"; using (var webResponse = SizewebRequest.GetResponse()) { var fileSize = webResponse.Headers.Get("Content-Length"); TotalBytesToReceive = Convert.ToDouble(fileSize); } response = request.GetResponse(); if (response != null) { // Once the WebResponse object has been retrieved, // get the stream object associated with the response's data remoteStream = response.GetResponseStream(); // Create the local file string filePath = TargetPath; localStream = File.Create(filePath); // Allocate a 1k buffer byte[] buffer = new byte[1024]; int bytesRead = 0; // Simple do/while loop to read from stream until // no bytes are returned do { // Read data (up to 1k) from the stream bytesRead = remoteStream.Read(buffer, 0, buffer.Length); // Write the data to the local file localStream.Write(buffer, 0, bytesRead); // Increment total bytes processed bytesProcessed += bytesRead; double bytesIn = double.Parse(bytesProcessed.ToString()); double percentage = bytesIn / TotalBytesToReceive * 100; percentage = Math.Round(percentage, 0); // Safe Update //Increment the progress bar if (progress.InvokeRequired) { progress.Invoke(new Action(() => progress.Value = int.Parse(Math.Truncate(percentage).ToString()))); } else { progress.Value = int.Parse(Math.Truncate(percentage).ToString()); } //Set the label progress Text if (label1.InvokeRequired) { label1.Invoke(new Action(() => label1.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString())); } else { label1.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString() + "%"; } } while (bytesRead > 0); } } } catch (Exception ex) { MessageBox.Show($"{ex}"); } finally { // Close the response and streams objects here // to make sure they're closed even if an exception // is thrown at some point if (response != null) { response.Close(); } if (remoteStream != null) { remoteStream.Close(); } if (localStream != null) { localStream.Close(); } } }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmSplashView)); this.label1 = new System.Windows.Forms.Label(); this.guna2ProgressBar1 = new Guna.UI2.WinForms.Guna2ProgressBar(); this.guna2CustomGradientPanel1 = new Guna.UI2.WinForms.Guna2CustomGradientPanel(); this.loading_timer = new System.Windows.Forms.Timer(this.components); this.guna2PictureBox1 = new Guna.UI2.WinForms.Guna2PictureBox(); this.guna2CustomGradientPanel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.guna2PictureBox1)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.BackColor = System.Drawing.Color.Transparent; this.label1.ForeColor = System.Drawing.Color.Black; this.label1.Location = new System.Drawing.Point(101, 449); this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(102, 13); this.label1.TabIndex = 4; this.label1.Text = "Cargando Sistema..."; // // guna2ProgressBar1 // this.guna2ProgressBar1.AutoRoundedCorners = true; this.guna2ProgressBar1.BackColor = System.Drawing.Color.Transparent; this.guna2ProgressBar1.BorderRadius = 5; this.guna2ProgressBar1.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); this.guna2ProgressBar1.GradientMode = System.Drawing.Drawing2D.LinearGradientMode.Horizontal; this.guna2ProgressBar1.Location = new System.Drawing.Point(101, 472); this.guna2ProgressBar1.Margin = new System.Windows.Forms.Padding(4); this.guna2ProgressBar1.Name = "guna2ProgressBar1"; this.guna2ProgressBar1.ProgressColor = System.Drawing.Color.White; this.guna2ProgressBar1.ProgressColor2 = System.Drawing.Color.White; this.guna2ProgressBar1.ShadowDecoration.Parent = this.guna2ProgressBar1; this.guna2ProgressBar1.Size = new System.Drawing.Size(541, 12); this.guna2ProgressBar1.TabIndex = 3; this.guna2ProgressBar1.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; // // guna2CustomGradientPanel1 // this.guna2CustomGradientPanel1.Controls.Add(this.guna2PictureBox1); this.guna2CustomGradientPanel1.Controls.Add(this.label1); this.guna2CustomGradientPanel1.Controls.Add(this.guna2ProgressBar1); this.guna2CustomGradientPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.guna2CustomGradientPanel1.FillColor2 = System.Drawing.Color.DodgerBlue; this.guna2CustomGradientPanel1.Location = new System.Drawing.Point(0, 0); this.guna2CustomGradientPanel1.Name = "guna2CustomGradientPanel1"; this.guna2CustomGradientPanel1.ShadowDecoration.Parent = this.guna2CustomGradientPanel1; this.guna2CustomGradientPanel1.Size = new System.Drawing.Size(757, 554); this.guna2CustomGradientPanel1.TabIndex = 5; this.guna2CustomGradientPanel1.Paint += new System.Windows.Forms.PaintEventHandler(this.guna2CustomGradientPanel1_Paint); // // loading_timer // this.loading_timer.Tick += new System.EventHandler(this.loading_timer_Tick_1); // // guna2PictureBox1 // this.guna2PictureBox1.BackColor = System.Drawing.Color.Transparent; this.guna2PictureBox1.Image = global::ProyectoGestionSach.Properties.Resources.GestionSachLogo; this.guna2PictureBox1.Location = new System.Drawing.Point(235, 146); this.guna2PictureBox1.Name = "guna2PictureBox1"; this.guna2PictureBox1.ShadowDecoration.Parent = this.guna2PictureBox1; this.guna2PictureBox1.Size = new System.Drawing.Size(300, 200); this.guna2PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.guna2PictureBox1.TabIndex = 5; this.guna2PictureBox1.TabStop = false; // // FrmSplashView // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(757, 554); this.Controls.Add(this.guna2CustomGradientPanel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "FrmSplashView"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "FrmSplashView"; this.guna2CustomGradientPanel1.ResumeLayout(false); this.guna2CustomGradientPanel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.guna2PictureBox1)).EndInit(); this.ResumeLayout(false); }