• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
灬伊天?
博客园    首页    新随笔    联系   管理    订阅  订阅

006-Download下载

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace SuperDLL
 6 {
 7     public static class Download
 8     {
 9         public static void DownloadFile(string URL, string fileName, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label)
10         {
11             float percent = 0;
12             try
13             {
14                 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
15                 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
16                 long totalBytes = myrp.ContentLength;
17                 if (prog != null)
18                 {
19                     prog.Maximum = (int)totalBytes;
20                 }
21                 System.IO.Stream st = myrp.GetResponseStream();
22                 System.IO.Stream so = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
23                 long totalDownloadedByte = 0;
24                 byte[] by = new byte[1024];
25                 int osize = st.Read(by, 0, (int)by.Length);
26                 while (osize > 0)
27                 {
28                     totalDownloadedByte = osize + totalDownloadedByte;
29                     System.Windows.Forms.Application.DoEvents();
30                     so.Write(by, 0, osize);
31                     if (prog != null)
32                     {
33                         prog.Value = (int)totalDownloadedByte;
34                     }
35                     osize = st.Read(by, 0, (int)by.Length);
36 
37                     percent = (float)totalDownloadedByte / (float)totalBytes * 100;
38                     label.Text = "当前补丁下载进度" + percent.ToString() + "%";
39                     System.Windows.Forms.Application.DoEvents();
40                 }
41                 so.Close();
42                 st.Close();
43             }
44             catch (System.Exception)
45             {
46 
47             }
48         }
49     }
50 }
 1 namespace ConsoleApplication_下载
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Loading();
 8         }
 9         public static void Loading()
10         {
11             try
12             {
13                 Console.Write(">");
14                 string url = Console.ReadLine();
15                 string[] array = url.Split('/');
16                 url = url.Replace(array[array.Length - 1], "");
17                 string time = DateTime.Now.ToString();
18                 time = time.Replace("/", "");
19                 time = time.Replace(":", "");
20                 time = time.Replace(" ", "");
21                 string path = currentPath + time + "\\";
22                 for (int i = 1; i <= 1000; i++)
23                 {
24                     string url2 = url + i + ".jpg";
25                     string path2 = path + i + ".jpg";
26                     HttpDownload(url2, path2);
27                     Console.WriteLine(url2 + "∽" + path2);
28                 }
29             }
30             catch (Exception)
31             {
32                 Loading();
33             }
34         }
35         public static string currentPath = System.Environment.CurrentDirectory + "\\";
36         public static void HttpDownload(string url, string path)
37         {
38             string tempPath = System.IO.Path.GetDirectoryName(path) + @"\Temp";
39             System.IO.Directory.CreateDirectory(tempPath);
40             string tempFile = tempPath + @"\" + System.IO.Path.GetFileName(path) + ".temp";
41             if (System.IO.File.Exists(tempFile))
42             {
43                 System.IO.File.Delete(tempFile);
44             }
45             try
46             {
47                 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
48                 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
49                 FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
50                 Stream responseStream = response.GetResponseStream();
51                 byte[] bArr = new byte[1024];
52                 int size = responseStream.Read(bArr, 0, (int)bArr.Length);
53                 while (size > 0)
54                 {
55                     fs.Write(bArr, 0, size);
56                     size = responseStream.Read(bArr, 0, (int)bArr.Length);
57                 }
58                 fs.Close();
59                 responseStream.Close();
60                 System.IO.File.Move(tempFile, path);
61             }
62             catch (Exception)
63             {
64                 Console.WriteLine("Copy......完成!");
65                 Loading();
66             }
67         }
68     }
69 }

 

posted @ 2018-09-08 19:38  灬伊天☂  阅读(218)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3