我用这个代码从网上下载。我想在1秒内测量下载文件的大小并显示它你可以帮我 [英] I use this code to download from the net.I want to measure the size of file to be downloaded in 1 second and display it you can help me

查看:53
本文介绍了我用这个代码从网上下载。我想在1秒内测量下载文件的大小并显示它你可以帮我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.Threading;

namespace SpeedTest
{
   
    class Program
    {
        
        static void Main(string[] args)
        {
            Console.Title = "A simple speedtest app";

            // the URL to download a file from
            Uri URL = new Uri("http://vodcdn.megatv.vn/vod/ducuong/Skyfall.2012.HD.flv");
            WebClient wc = new WebClient();

            Console.WriteLine("Welcome to this simple speedtest,\nwhich will test your download rate.");
            Console.WriteLine("Press any key to begin.");
            Console.ReadKey();

            Console.WriteLine("\nDownloading file: ...");
            Console.WriteLine("From http://vodcdn.megatv.vn/vod/ducuong/Skyfall.2012.HD.flv");
            Console.WriteLine("Note: This file will automatically be deleted after the test.");
           
            wc.DownloadFile(URL, @"string url = HttpContext.Current.Request.Url.AbsoluteUri;");
    
            //long size = wc.Length;
          //  Console.WriteLine("File Size in Bytes: {0}", size);
          
            Console.WriteLine("\nPress any key to exit...");
            Console.Read();
            Console.WriteLine("Cleaning up... (deleting downloaded file)");
            try
            {
                // delete downloaded file
                System.IO.File.Delete(@"string url = HttpContext.Current.Request.Url.AbsoluteUri;");
                Console.WriteLine("Done.");
            }
            catch
            {
                Console.WriteLine("Couldn't delete download file.");
                Console.WriteLine("To delete the file yourself, go to your C-drive and look for the file 'speedtest.txt'.");
                Console.ReadKey();
            } 
        }


        
    }
}
<pre lang="c#">



我使用此代码从网上下载。我想在1秒内测量要下载的文件大小并显示它你可以帮助我


I use this code to download from the net.I want to measure the size of file to be downloaded in 1 second and display it you can help me

推荐答案

使用WebClient执行此操作的唯一方法是使用WebClient.DownloadFileAsync异步下载文件,并附加到事件WebClient.DownloadFileCompleted和WebClient.DownloadFileProgressChanged。在progress changed事件中,您可以读取接收的字节数,下载的百分比以及要接收的总字节数。如果你想测量你得到多少时间,你可以添加一个计时器作为你班级的成员变量,并读取/重置进度改变事件中的值。
The only way to do it (with WebClient) is to download the file asynchronously with the WebClient.DownloadFileAsync and attach to the events WebClient.DownloadFileCompleted and WebClient.DownloadFileProgressChanged. In the progress changed event, you can read the number of bytes received, the percentage downloaded, and the total bytes to be received. If you want to measure how much over time you get, you can add a timer as a member variable of your class and read/reset the value in the progress changed event.


这篇关于我用这个代码从网上下载。我想在1秒内测量下载文件的大小并显示它你可以帮我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆