DownloadTools.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. namespace FantasySolution.FantasyCorrector
  5. {
  6. public class DownloadTools
  7. {
  8. //11111333
  9. public string DownloadFile(string SavePath, string DownloadUrl, string FileName)
  10. {
  11. if (!Directory.Exists(SavePath))
  12. {
  13. Directory.CreateDirectory(SavePath);
  14. }
  15. string path = SavePath + "\\" + FileName;
  16. string requestUriString = DownloadUrl + "/" + FileName;
  17. try
  18. {
  19. WebResponse response = WebRequest.Create(requestUriString).GetResponse();
  20. if (response.ContentLength > 0)
  21. {
  22. try
  23. {
  24. long contentLength = response.ContentLength;
  25. try
  26. {
  27. Stream responseStream = response.GetResponseStream();
  28. StreamReader reader = new StreamReader(responseStream);
  29. byte[] buffer = new byte[contentLength];
  30. int count = buffer.Length;
  31. int offset = 0;
  32. while (contentLength > 0)
  33. {
  34. int num4 = responseStream.Read(buffer, offset, count);
  35. if (num4 == 0)
  36. {
  37. break;
  38. }
  39. offset += num4;
  40. count -= num4;
  41. float num5 = ((float)offset) / 1024f;
  42. float num6 = ((float)buffer.Length) / 1024f;
  43. Convert.ToInt32((float)((num5 / num6) * 100f));
  44. }
  45. FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  46. stream2.Write(buffer, 0, buffer.Length);
  47. responseStream.Close();
  48. reader.Close();
  49. stream2.Close();
  50. return FileName;
  51. }
  52. catch
  53. {
  54. return null;
  55. }
  56. }
  57. catch
  58. {
  59. return null;
  60. }
  61. }
  62. }
  63. catch
  64. {
  65. return null;
  66. }
  67. return null;
  68. }
  69. public bool IsDownloadFile(string SavePath, string DownloadUrl, string FileName)
  70. {
  71. if (!Directory.Exists(SavePath))
  72. {
  73. Directory.CreateDirectory(SavePath);
  74. }
  75. string path = SavePath + "\\" + FileName;
  76. string requestUriString = DownloadUrl + "/" + FileName;
  77. try
  78. {
  79. WebResponse response = WebRequest.Create(requestUriString).GetResponse();
  80. if (response.ContentLength > 0)
  81. {
  82. try
  83. {
  84. long contentLength = response.ContentLength;
  85. try
  86. {
  87. Stream responseStream = response.GetResponseStream();
  88. StreamReader reader = new StreamReader(responseStream);
  89. byte[] buffer = new byte[contentLength];
  90. int count = buffer.Length;
  91. int offset = 0;
  92. while (contentLength > 0)
  93. {
  94. int num4 = responseStream.Read(buffer, offset, count);
  95. if (num4 == 0)
  96. {
  97. break;
  98. }
  99. offset += num4;
  100. count -= num4;
  101. float num5 = ((float)offset) / 1024f;
  102. float num6 = ((float)buffer.Length) / 1024f;
  103. Convert.ToInt32((float)((num5 / num6) * 100f));
  104. }
  105. FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  106. stream2.Write(buffer, 0, buffer.Length);
  107. responseStream.Close();
  108. reader.Close();
  109. stream2.Close();
  110. return true;
  111. }
  112. catch
  113. {
  114. return false;
  115. }
  116. }
  117. catch
  118. {
  119. return false;
  120. }
  121. }
  122. }
  123. catch
  124. {
  125. return false;
  126. }
  127. return false;
  128. }
  129. }
  130. }