用 C# 下載圖片檔案
在用 C# 寫下載網頁或檔案時,都會使用 HttpClient 這個類別,但下載圖檔時偏偏特別麻煩,還不如以前的方式好用,本篇特地做個筆記,雖然已經是過時的方式,但因為好用所以暫時還是不會放棄。程式碼如下:
using System.Net;
string url = "http://www.example.com/test.jpg";
string filePath = "test.jpg";
WebClient client = new ();
client.DownloadFile(url, filePath);
就是這麼簡單,如果用 HttpClient,步驟多又很煩,所以不是長期維護的專案,用 WebClient 還是不錯的方法。
參考資料