information from download

This is a discussion on information from download within the C# Programming forums, part of the General Programming Boards category; How would i get the download rate and the size of the file that i'm downloading using this command: PHP ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    16

    Question information from download

    How would i get the download rate and the size of the file that i'm downloading using this command:
    PHP Code:
            private void botao1_Click(object senderEventArgs e)
            {

                
    string url "http://mysite.com/haha.mst";
                
    string destino "C:\\Programas\\haha.mst";
                
    System.Net.WebClient Client = new System.Net.WebClient();
                
    Client.DownloadFile(urldestino);
             } 

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I don't know of any way to get the download rate and/or the download file size using the DownloadFile method. So, I'll post another approach to your request. You'll have to thoroughly test it since I've done very little testing on the code.

    Also, please note the space in the following code:
    Code:
    FileWriter=new StreamWriter(new FileStream(destino,FileMode.CreateNew,FileAccess.Write,FileShare.None));
    The message board sometimes puts random spaces in the code as indicated above which makes it appear as a typo error. The posted code should NOT have a space between the W and the r

    Code:
    using System;
    using System.Web;
    using System.IO;
    using System.Net;
    using System.Collections;
    
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
            static void Main()
            {
            string url =   "http://mysite.com/haha.mst"; 
            string destino = "C:\\temp\\teste.mst"; 
            int iLength = 1024;
            char [] Buffer = new char[1025];
            int iBytesRead = 0;
            long lTotalBytesRead = 0;
            StreamWriter FileWriter=null;
            double  dBytesPerMinute;
            TimeSpan elapsed;
            try
            {
                //Create the request object
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.AllowAutoRedirect=true;
                //Create the response object
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine("Response Header {0} ", response.Headers);
                Console.WriteLine("Response ContentType {0} ", response.ContentType);
                long lFileSizeInBytes = Convert.ToInt32(response.ContentLength);
                FileInfo TheFile = new FileInfo(destino);
                if (TheFile.Exists)
                    File.Delete(destino);
                DateTime startTime = DateTime.Now;
                FileWriter=new StreamWriter(new FileStream(destino,FileMode.CreateNew,FileAccess.Write,FileShare.None)); 
                StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
                iBytesRead = sr.Read( Buffer, 0, iLength);
                FileWriter.Write(Buffer,0, iBytesRead);
                while( iBytesRead > 0 )
                {
                    iBytesRead = sr.Read( Buffer, 0, iLength);
                    if(iBytesRead == 0)
                        break;
                    FileWriter.Write(Buffer,0, iBytesRead);
                    lTotalBytesRead += iBytesRead;
                    double percent = ((double)lTotalBytesRead /(double)lFileSizeInBytes) * 100.0;
                    elapsed = DateTime.Now - startTime;
                    if(elapsed.TotalMinutes > 1.0)
                    {
                        dBytesPerMinute = (((double)lTotalBytesRead  / (double)elapsed.TotalMinutes ) / 1024);
                        Console.WriteLine("Bytes copied per minute: " +dBytesPerMinute.ToString("###,###,###KB"));
                    }
                    Console.WriteLine("Percent Copied:" + percent.ToString("###.##") + "%");
                }
                elapsed = DateTime.Now - startTime;
                Console.WriteLine("hours:" + elapsed.Hours);
                Console.WriteLine("minutes:" + elapsed.Minutes);
                Console.WriteLine("seconds:" + elapsed.Seconds);
            }
            catch (WebException WebExcp)
            {
                Console.WriteLine("A WebException has been caught!");
                Console.WriteLine(WebExcp.ToString());
                Console.WriteLine();
            }
            finally
            {
                if (FileWriter!=null)
                {
                    FileWriter.Close();
                }
            }
        }
    }

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    16
    Yeah, it works, but i would like to do it i a windows from, cause my old code was made in DOS. I would like a design u know

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Basic Windows Form Internet File downloader app with progress bar. Very little error checking. Fleshing out the app with error checking etc. is up to you.

    Have fun

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;
    using System.Diagnostics;
    using System.IO;
    using System.Net;
    
    namespace Miranda.DownloadProcess
    {
        public class Form1 : System.Windows.Forms.Form
        {
            private System.ComponentModel.Container components = null;
            private System.Windows.Forms.Button btnStopThread;
            private System.Windows.Forms.Button btnStartThread;
            private System.Windows.Forms.ProgressBar progressBar1;
            private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.TextBox textBoxURL;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.TextBox textBoxFile;
            private System.Windows.Forms.Label textBytesCopied;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Label textFileSize;
            private System.Windows.Forms.Label label4;
            private Thread timerThread;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null) 
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }
    
            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.btnStopThread = new System.Windows.Forms.Button();
                this.btnStartThread = new System.Windows.Forms.Button();
                this.progressBar1 = new System.Windows.Forms.ProgressBar();
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.textBytesCopied = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.textBoxURL = new System.Windows.Forms.TextBox();
                this.textBoxFile = new System.Windows.Forms.TextBox();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.textFileSize = new System.Windows.Forms.Label();
                this.label4 = new System.Windows.Forms.Label();
                this.groupBox1.SuspendLayout();
                this.SuspendLayout();
                // 
                // btnStopThread
                // 
                this.btnStopThread.Location = new System.Drawing.Point(352, 112);
                this.btnStopThread.Name = "btnStopThread";
                this.btnStopThread.Size = new System.Drawing.Size(133, 23);
                this.btnStopThread.TabIndex = 7;
                this.btnStopThread.Text = "Abort download";
                this.btnStopThread.Click += new System.EventHandler(this.btnStopThread_Click);
                // 
                // btnStartThread
                // 
                this.btnStartThread.Location = new System.Drawing.Point(80, 112);
                this.btnStartThread.Name = "btnStartThread";
                this.btnStartThread.Size = new System.Drawing.Size(133, 23);
                this.btnStartThread.TabIndex = 6;
                this.btnStartThread.Text = "Start download";
                this.btnStartThread.Click += new System.EventHandler(this.btnStartThread_Click);
                // 
                // progressBar1
                // 
                this.progressBar1.Location = new System.Drawing.Point(16, 24);
                this.progressBar1.Name = "progressBar1";
                this.progressBar1.Size = new System.Drawing.Size(528, 24);
                this.progressBar1.Step = 1;
                this.progressBar1.TabIndex = 5;
                // 
                // groupBox1
                // 
                this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.label4,
                        this.textFileSize,
                        this.textBytesCopied,
                        this.btnStopThread,
                        this.btnStartThread,
                        this.progressBar1,
                        this.label3}
                );
                this.groupBox1.Location = new System.Drawing.Point(24, 128);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(568, 152);
                this.groupBox1.TabIndex = 6;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = "Download Progress";
                // 
                // textBytesCopied
                // 
                this.textBytesCopied.Location = new System.Drawing.Point(128, 64);
                this.textBytesCopied.Name = "textBytesCopied";
                this.textBytesCopied.Size = new System.Drawing.Size(100, 16);
                this.textBytesCopied.TabIndex = 8;
                // 
                // label3
                // 
                this.label3.Location = new System.Drawing.Point(32, 64);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(80, 16);
                this.label3.TabIndex = 11;
                this.label3.Text = "Bytes Copied:";
                // 
                // textBoxURL
                // 
                this.textBoxURL.Location = new System.Drawing.Point(104, 40);
                this.textBoxURL.Name = "textBoxURL";
                this.textBoxURL.Size = new System.Drawing.Size(480, 20);
                this.textBoxURL.TabIndex = 7;
                this.textBoxURL.Text = "";
                // 
                // textBoxFile
                // 
                this.textBoxFile.Location = new System.Drawing.Point(104, 80);
                this.textBoxFile.Name = "textBoxFile";
                this.textBoxFile.Size = new System.Drawing.Size(480, 20);
                this.textBoxFile.TabIndex = 8;
                this.textBoxFile.Text = "";
                // 
                // label1
                // 
                this.label1.Location = new System.Drawing.Point(64, 48);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(32, 16);
                this.label1.TabIndex = 9;
                this.label1.Text = "URL:";
                // 
                // label2
                // 
                this.label2.Location = new System.Drawing.Point(64, 88);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(24, 16);
                this.label2.TabIndex = 10;
                this.label2.Text = "File:";
                // 
                // textFileSize
                // 
                this.textFileSize.Location = new System.Drawing.Point(416, 64);
                this.textFileSize.Name = "textFileSize";
                this.textFileSize.Size = new System.Drawing.Size(100, 16);
                this.textFileSize.TabIndex = 12;
                // 
                // label4
                // 
                this.label4.Location = new System.Drawing.Point(344, 64);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(56, 16);
                this.label4.TabIndex = 13;
                this.label4.Text = "File Size:";
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(624, 325);
                this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.label2,
                        this.label1,
                        this.textBoxFile,
                        this.textBoxURL,
                        this.groupBox1}
                );
                this.Name = "Form1";
                this.Text = "Miranda\'s Download";
                this.groupBox1.ResumeLayout(false);
                this.ResumeLayout(false);
    
            }
            #endregion
    
            // The main entry point for the application.
            [STAThread]
                static void Main() 
                {
                Application.Run(new Form1());
            }
    
            // Start the background thread to update the progress bar
            private void btnStartThread_Click(object sender, System.EventArgs e)
            {
                StopThread();
                timerThread = new Thread(new ThreadStart(ThreadProc));
                timerThread.IsBackground = true;
                timerThread.Start();
            }
    
            private void btnStopThread_Click(object sender, System.EventArgs e)
            {
                StopThread();
            }
            public void ThreadProc() 
            {
                try 
                {
                    MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
                    int iLength = 1024;
                    char [] Buffer = new char[1025];
                    int iBytesRead = 0;
                    long lTotalBytesRead = 0;
                    StreamWriter FileWriter=null;
                    TimeSpan elapsed;
                    double index = 2.0;
                    try
                    {
                        //Create the request object
                        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(textBoxURL.Text);
                        request.AllowAutoRedirect=true;
                        //Create the response object
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        Console.WriteLine("Response Header {0} ", response.Headers);
                        Console.WriteLine("Response ContentType {0} ", response.ContentType);
                        long lFileSizeInBytes = Convert.ToInt32(response.ContentLength);
                        textFileSize.Text = lFileSizeInBytes.ToString("###,###,###,###");   
                        progressBar1.Maximum = 100;
                        progressBar1.Minimum = 1;
                        FileInfo TheFile = new FileInfo(textBoxFile.Text);
                        if (TheFile.Exists)
                            File.Delete(textBoxFile.Text);
                        DateTime startTime = DateTime.Now;
                        FileWriter=new StreamWriter(new FileStream(textBoxFile.Text,FileMode.CreateNew,FileAccess.Write,FileShare.None)); 
                        StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
                        iBytesRead = sr.Read( Buffer, 0, iLength);
                        FileWriter.Write(Buffer,0, iBytesRead);
                        lTotalBytesRead += iBytesRead;
                        while( iBytesRead > 0 )
                        {
                            iBytesRead = sr.Read( Buffer, 0, iLength);
                            FileWriter.Write(Buffer,0, iBytesRead);
                            lTotalBytesRead += iBytesRead;
                            textBytesCopied.Text = lTotalBytesRead.ToString("###,###,###,###");
                            double percent = ((double)lTotalBytesRead /(double)lFileSizeInBytes) * 100.0;
                            if(percent >= index)
                            {
                                index = index+1.0;
                                this.BeginInvoke(mi);
                            }   
                            elapsed = DateTime.Now - startTime;
                        }
                        elapsed = DateTime.Now - startTime;
                        MessageBox.Show("Download Completed!","Miranda's Download",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    catch (WebException WebExcp)
                    {
                        MessageBox.Show(WebExcp.ToString(), "WebExeption has bee caught!",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    finally
                    {
                        if (FileWriter!=null)
                        {
                            FileWriter.Close();
                        }
                    }
                }
                catch (ThreadInterruptedException) 
                {
                    // Just exit
                }
                catch (Exception) 
                {
                }
            }
            private void UpdateProgress() 
            {
                // Reset to start if required
                if (progressBar1.Value == progressBar1.Maximum) 
                {
                    progressBar1.Value = progressBar1.Minimum ;
                }
                progressBar1.PerformStep() ;
            }
            private void StopThread()
            {
                if (timerThread != null)
                {
                    timerThread.Interrupt();
                    timerThread = null;
                }
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Assignment Help !! (Student information system)
    By ashb in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 04:32 AM
  3. Which ISOs To Download?
    By Rouss in forum Tech Board
    Replies: 8
    Last Post: 04-30-2004, 08:02 PM
  4. download free bsd image?
    By iain in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-11-2002, 10:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21