12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace LYFZ.Network
- {
- /// <summary>
- /// SendStatus类,即时计算发送文件的状态
- /// 用来记录和打印发送完成的状态,已经发送了多少字节,完成度是百分之多少,等等
- /// </summary>
- public class TCP_SendStatus
- {
- private FileInfo info;
- private long fileBytes;
- public TCP_SendStatus(string filePath)
- {
- info = new FileInfo(filePath);
- fileBytes = info.Length;
- }
- private ServerFileInfo sFileInfo;
- public TCP_SendStatus(ServerFileInfo fileInfo)
- {
- sFileInfo = fileInfo;
- fileBytes = fileInfo.FileSize;
- }
- /// <summary>
- /// 获取发送状态
- /// </summary>
- /// <param name="sent">已发送字节数</param>
- public string GetStatus(int sent)
- {
- string percent = GetPercent(sent);
- return String.Format("发送{0}字节,{1}%...", sent, percent);
- }
-
- /// <summary>
- /// 获得文件发送的百分比
- /// </summary>
- /// <param name="sent"></param>
- /// <returns></returns>
- public string GetPercent(int sent)
- {
- try
- {
- decimal allBytes = Convert.ToDecimal(fileBytes);
- decimal currentSent = Convert.ToDecimal(sent);
- decimal percent = (currentSent / allBytes) * 100;
- percent = Math.Round(percent, 1); //保留一位小数
- try
- {
- percentInt = Convert.ToInt32(percent * 10);
- }
- catch { }
- if (percent.ToString() == "100.0")
- return "100";
- else
- return percent.ToString();
- }
- catch {
- return "0";
- }
- }
- int percentInt=0;
- /// <summary>
- /// 报告进度值 0-1000的整数
- /// </summary>
- public int PercentInt
- {
- get
- {
- return percentInt;
- }
- }
- }
- }
|