一、概述
批量下載圖片是我們在日常開發中經常會遇到的一個需求,這不,最近工作中就需要批量下載圖片到本地,先是通過Excel拼接生成了所有鏈接地址,然后想到的是通過下載軟件來批量下載??墒且幌氲接忠〞r間去查找、安裝、研究軟件,不如自己寫個來的快。
以下是使用C#開發的控制臺程序代碼,通過循環讀取文本文件中每一行地址字符串,執行下載并保存到本地文件夾中。
下面話不多說了,來一起看看詳細的介紹吧
二、C#實例代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
//using System; //using System.Net; //using System.Text; //using System.IO; //-------------------------------------------- static void Main( string [] args) { //StreamReader讀取 int count = 0; using (Stream readerStream = new FileStream( @"d:\list.txt" , FileMode.Open)) using (StreamReader reader = new StreamReader(readerStream, Encoding.UTF8)) using (WebClient client = new WebClient()) { string line; while ((line = reader.ReadLine()) != null ) { count++; Console.WriteLine(line + " " + count); Uri uri = new Uri(line); if (uri!= null ) { string filename = Path.GetFileName(uri.LocalPath); client.DownloadFile(uri, @"c:\pictures\" +filename); Console.WriteLine( "文件:" +filename+ " 下載成功!" + " 計數:" + count); } else { Console.WriteLine( "路徑:" + line + " 不是下載地址!失敗序號:" +count ); } } } Console.WriteLine( "下載完成!" ); Console.ReadKey(); } |
三、參考文章
How to download image from url
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://renqiancheng.com/2018/11/C-批量下載圖片到本地.html