コード例 #1
0
        //We store downloaded files in C:\TestDir (hardcoded in the Record class below)
        public Form1()
        {
            InitializeComponent();
            recordQ = new Queue <Record>();
            //replace the URI string below. Nothing else to replace.
            //recordQ.Enqueue(new Record(@"URI1", "SQLtraining.exe"));
            //recordQ.Enqueue(new Record(@"URI2", "Project Mgmt.pptx"));

            //first uri to process. Second param is the file name that we store.
            Record record = new Record(@"URI0", "Agile.pptx"); // replace the URI

            //Initialize a webclient and download the first record
            using (wc = new WebClient())
            {
                counter = new NetSpeedCounter(wc);
                wc.DownloadFileCompleted += (sender, e) =>
                {
                    if (recordQ.Count == 0)
                    {
                        UpdateStatusText("Done");
                        return;
                    }
                    var nr = recordQ.Dequeue();
                    //just create directory. the code uses the same directory
                    CheckAndCreate(nr.Directory);
                    //need not even use invoke here. Just a plain method call will suffice.
                    this.Invoke((MethodInvoker) delegate
                    {
                        UpdateStatusText("Left to process: " + recordQ.Count + " files");
                    });
                    counter.Reset();
                    counter.Start();
                    //continue with rest of records
                    wc.DownloadFileAsync(nr.DownloadPath, nr.GetFullPath());
                    this.lblFile.Text = nr.DownloadPath.OriginalString;
                };
                //just update speed in UI
                wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                counter.Start();
                //display URI we are downloading
                this.lblFile.Text = record.DownloadPath.OriginalString;
                //start first download
                wc.DownloadFileAsync(record.DownloadPath, record.GetFullPath());
            }
        }
コード例 #2
0
        public Form1()
        {
            InitializeComponent();
            recordQ = new Queue <Record>();
            //replace the URI string
            //recordQ.Enqueue(new Record(@"URI1", "SQLtraining.exe"));
            //recordQ.Enqueue(new Record(@"URI2", "Project Mgmt.pptx"));

            //first uri to process
            Record record = new Record(@"URI0", "Agile.pptx"); // replace the URI

            //Initialize a webclient and download the first record
            using (wc = new WebClient())
            {
                counter = new NetSpeedCounter(wc);
                wc.DownloadFileCompleted += (sender, e) =>
                {
                    if (recordQ.Count == 0)
                    {
                        UpdateStatusText("Done");
                        return;
                    }
                    var nr = recordQ.Dequeue();
                    //just create directory. the code uses the same directory
                    CheckAndCreate(nr.Directory);

                    this.Invoke((MethodInvoker) delegate
                    {
                        UpdateStatusText("Left to process: " + recordQ.Count + " files");
                    });
                    counter.Reset();
                    counter.Start();
                    //continue with rest of records
                    wc.DownloadFileAsync(nr.DownloadPath, nr.Directory + nr.File);
                    this.lblFile.Text = nr.DownloadPath.OriginalString;
                };
                wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                counter.Start();
                this.lblFile.Text = record.DownloadPath.OriginalString;
                wc.DownloadFileAsync(record.DownloadPath, record.Directory + record.File);
            }
        }