TCP_SendStatus.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace LYFZ.Network
  7. {
  8. /// <summary>
  9. /// SendStatus类,即时计算发送文件的状态
  10. /// 用来记录和打印发送完成的状态,已经发送了多少字节,完成度是百分之多少,等等
  11. /// </summary>
  12. public class TCP_SendStatus
  13. {
  14. private FileInfo info;
  15. private long fileBytes;
  16. public TCP_SendStatus(string filePath)
  17. {
  18. info = new FileInfo(filePath);
  19. fileBytes = info.Length;
  20. }
  21. private ServerFileInfo sFileInfo;
  22. public TCP_SendStatus(ServerFileInfo fileInfo)
  23. {
  24. sFileInfo = fileInfo;
  25. fileBytes = fileInfo.FileSize;
  26. }
  27. /// <summary>
  28. /// 获取发送状态
  29. /// </summary>
  30. /// <param name="sent">已发送字节数</param>
  31. public string GetStatus(int sent)
  32. {
  33. string percent = GetPercent(sent);
  34. return String.Format("发送{0}字节,{1}%...", sent, percent);
  35. }
  36. /// <summary>
  37. /// 获得文件发送的百分比
  38. /// </summary>
  39. /// <param name="sent"></param>
  40. /// <returns></returns>
  41. public string GetPercent(int sent)
  42. {
  43. try
  44. {
  45. decimal allBytes = Convert.ToDecimal(fileBytes);
  46. decimal currentSent = Convert.ToDecimal(sent);
  47. decimal percent = (currentSent / allBytes) * 100;
  48. percent = Math.Round(percent, 1); //保留一位小数
  49. try
  50. {
  51. percentInt = Convert.ToInt32(percent * 10);
  52. }
  53. catch { }
  54. if (percent.ToString() == "100.0")
  55. return "100";
  56. else
  57. return percent.ToString();
  58. }
  59. catch {
  60. return "0";
  61. }
  62. }
  63. int percentInt=0;
  64. /// <summary>
  65. /// 报告进度值 0-1000的整数
  66. /// </summary>
  67. public int PercentInt
  68. {
  69. get
  70. {
  71. return percentInt;
  72. }
  73. }
  74. }
  75. }