[C#] 复制文件并计算传输速度
来源:互联网 发布:积家约会 知乎 编辑:程序博客网 时间:2024/06/05 19:45
原理:
在复制文件之前读取一个系统时间, 复制完之后再读取系统时间, 然后计算 时间间隔, 用文件的大小除以时间得到传输速度
使用方法:
创建一个大于2G的文件,将其命名为"1.zip", 并将其与测试工具放在同一个文件夹里,然后运行程序.
在下拉列表框里选择盘符后, 点击 开始 按钮, 等3-4分钟, 就可以得到结果.(参考下面的结果图.)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace SpeedTestTool{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { lblPrompt.Text = ""; //获得系统盘符,并将其显示在combobox里, 以供选择 DriveInfo[] allDrivers = DriveInfo.GetDrives(); foreach(DriveInfo d in allDrivers) { cmbPartition.Items.Add(d.Name); } } private void btnStart_Click(object sender, EventArgs e) { string surPath, desPath,ts,ts_h,ts_m,ts_s,strPartition; double t,speed; DateTime dt1, dt2; btnStart.Enabled = false; if (cmbPartition.SelectedIndex== -1) { //如果没有选择盘符, 则给出错误信息 MessageBox.Show("Please Select Target Partition firstly!","Error Message",MessageBoxButtons.OK,MessageBoxIcon.Error); } else { strPartition = cmbPartition.SelectedItem.ToString();//获得选择的系统盘符 //MessageBox.Show(strPartition); //定义源文件的路径 surPath = Directory.GetCurrentDirectory() + "\\1.zip"; //MessageBox.Show(surPath); //定义目标文件夹的路径 desPath = strPartition + "test"; //MessageBox.Show(desPath); //判断目标文件夹是否存在,如果不存在,就创建目标文件夹 if (!Directory.Exists(desPath)) { try { Directory.CreateDirectory(desPath); } catch (Exception error) { MessageBox.Show(error.Message); } } //判断目标文件是否存大,如果存在,就删除. if (File.Exists(desPath + "\\1.zip")) { try { File.Delete(desPath + "\\1.zip"); } catch(Exception error) { MessageBox.Show(error.Message); } } try { //开始复制文件之前,获得系统当前时间,将在txtBeginTime里显示出来. dt1 = DateTime.Now; txtBeginTime.Text = dt1.ToLongTimeString(); //复制文件 File.Copy(surPath, desPath + "\\1.zip"); //复制文件成功后,获得系统当前时间,将在txtEndTime里显示出来. dt2 = DateTime.Now; txtEndTime.Text = dt2.ToLongTimeString(); //计算间隔时间,并在txtInterval中显示出来 ts = (dt2 - dt1).ToString(); string[] strs = ts.Split(':'); ts_h = strs[0]; ts_m = strs[1]; ts_s = strs[2]; t = Convert.ToDouble(ts_s) + Convert.ToDouble(ts_m) * 60 + Convert.ToDouble(ts_h) * 3600; txtInterval.Text = (Convert.ToInt32(t)).ToString() + " s"; //得到文件的大小, 并在txtFileSize中显示出来 FileInfo fi = new FileInfo(desPath + "\\1.zip"); txtFileSize.Text = ((fi.Length) / (1024 * 1024)).ToString()+ " MB"; //计算速度并显示在txtSpeed中. speed = ((fi.Length) / (1024 * 1024)) / t; txtSpeed.Text = (Convert.ToInt32(speed)).ToString()+" MB/s"; //没完成的时候,给出提示 lblPrompt.ForeColor = Color.Green; lblPrompt.Text = "Complete!"; //让start按钮可以用 btnStart.Enabled = true; } catch (Exception error) { MessageBox.Show(error.Message,"Error Message",MessageBoxButtons.OK,MessageBoxIcon.Error); } } } }}
运行程序如下图:
计算结果后如下图:
- [C#] 复制文件并计算传输速度
- 网络传输速度计算
- TCP传输速度的计算
- 复制文件速度参考
- TCP传输过程复制文件
- [C#] 筛选复制文件并保持原有的目录结构
- 读取并复制文件
- 远程Windows并传输文件
- 加快系统文件复制速度必杀技
- FileInputStream复制文件,实时显示速度
- Linux C语言下TCP传文件,并显示传输进度条
- Java读取并复制文件
- 【dd】转换并复制文件
- 批量复制文件并改名
- DVB-C DEMOD 传输速率计算
- C入门:文件复制
- C 语言 文件复制
- 复制文件--c
- Delphi中StrToDateTime函数TFormatSettings参数的使用
- [iPhone开发项目] 团购信息
- 百度地图查询周围建筑
- C++ 强制类型转换
- 继承学习2
- [C#] 复制文件并计算传输速度
- ubuntu cd安装的一个问题
- CSS圆角效果 -webkit-border-radius(CSS3中border-radius隐藏的威力)
- 使用Apache commons-codec Base64实现加解密
- MyEclipse的快捷使用(含关联源码和Doc的方式)
- sscanf,sscanf_s及其相关用法
- 不用css样式表和背景图片实现圆角矩形,超简洁!
- 学习android的步骤
- linux下新建用户和组