[C#][winForm] 從資料夾裡的文字檔內容逐一下載圖片
3 min readDec 24, 2018
使用情境是已經把檔案上傳到storage,並且路徑複寫到一份log文字檔,但是不確定上傳的檔案是否都成功,所以依上傳log文字檔去逐一進行下載每個檔案,再來核對上傳數量與下載數量是否一致,以及檔案有無狀況。
文字檔內容如下,每行逐一下載到本機
var files = from file in Directory.EnumerateFiles(System.IO.Directory.GetCurrentDirectory() + @"\img", "*.*", SearchOption.AllDirectories)
from line in File.ReadLines(file)
where line.Contains("https://blob.vivotek.com")
select new
{
File = file,
Line = line
};foreach (var f in files)
{
Console.WriteLine("{0}\t{1}", f.File, f.Line);
WebClient client = new WebClient();
Stream stream = client.OpenRead(f.Line);int i = 1; // 檔名
using (var yourImage = Image.FromStream(stream))
{
if (f.Line.Contains(".png"))
yourImage.Save(Directory.GetCurrentDirectory() + @"\image\"+i.ToString()+".png", ImageFormat.Png);
else if (f.Line.Contains(".jpg"))
yourImage.Save(Directory.GetCurrentDirectory() + @"\image\" + i.ToString() + ".jpg", ImageFormat.Jpeg);
else if (f.Line.Contains(".gif"))
yourImage.Save(Directory.GetCurrentDirectory() + @"\image\" + i.ToString() + ".gif", ImageFormat.Gif);
}
i++;
stream.Flush();
stream.Close();
}