| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System;
- using System.IO;
- using System.Net;
- namespace FantasySolution.FantasyCorrector
- {
- public class DownloadTools
- {
- //11111333
- public string DownloadFile(string SavePath, string DownloadUrl, string FileName)
- {
- if (!Directory.Exists(SavePath))
- {
- Directory.CreateDirectory(SavePath);
- }
- string path = SavePath + "\\" + FileName;
- string requestUriString = DownloadUrl + "/" + FileName;
- try
- {
- WebResponse response = WebRequest.Create(requestUriString).GetResponse();
- if (response.ContentLength > 0)
- {
- try
- {
- long contentLength = response.ContentLength;
- try
- {
- Stream responseStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(responseStream);
- byte[] buffer = new byte[contentLength];
- int count = buffer.Length;
- int offset = 0;
- while (contentLength > 0)
- {
- int num4 = responseStream.Read(buffer, offset, count);
- if (num4 == 0)
- {
- break;
- }
- offset += num4;
- count -= num4;
- float num5 = ((float)offset) / 1024f;
- float num6 = ((float)buffer.Length) / 1024f;
- Convert.ToInt32((float)((num5 / num6) * 100f));
- }
- FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
- stream2.Write(buffer, 0, buffer.Length);
- responseStream.Close();
- reader.Close();
- stream2.Close();
- return FileName;
- }
- catch
- {
- return null;
- }
- }
- catch
- {
- return null;
- }
- }
- }
- catch
- {
- return null;
- }
- return null;
- }
- public bool IsDownloadFile(string SavePath, string DownloadUrl, string FileName)
- {
- if (!Directory.Exists(SavePath))
- {
- Directory.CreateDirectory(SavePath);
- }
- string path = SavePath + "\\" + FileName;
- string requestUriString = DownloadUrl + "/" + FileName;
- try
- {
- WebResponse response = WebRequest.Create(requestUriString).GetResponse();
- if (response.ContentLength > 0)
- {
- try
- {
- long contentLength = response.ContentLength;
- try
- {
- Stream responseStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(responseStream);
- byte[] buffer = new byte[contentLength];
- int count = buffer.Length;
- int offset = 0;
- while (contentLength > 0)
- {
- int num4 = responseStream.Read(buffer, offset, count);
- if (num4 == 0)
- {
- break;
- }
- offset += num4;
- count -= num4;
- float num5 = ((float)offset) / 1024f;
- float num6 = ((float)buffer.Length) / 1024f;
- Convert.ToInt32((float)((num5 / num6) * 100f));
- }
- FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
- stream2.Write(buffer, 0, buffer.Length);
- responseStream.Close();
- reader.Close();
- stream2.Close();
- return true;
- }
- catch
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- }
- catch
- {
- return false;
- }
- return false;
- }
- }
- }
|