c#下载图片到指定目录

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/feitiandezhu1399/article/details/84371131
 string path ="http://www.quanjing.com/imgbuy/QJ9107100567.png";//下载图片的地址
 string newPath = string.Format(@"D:\UPLOAD\");//目标地址
               
 string ImsFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; //目标文件名
              
 System.Net.WebRequest imgRequest = System.Net.WebRequest.Create(path);
 HttpWebResponse res;
 try {
       res = (HttpWebResponse)imgRequest.GetResponse();
 }catch(WebException ex){
       res = (HttpWebResponse)ex.Response;
 }
 if( res.StatusCode.ToString() == "OK" ){
      System.Drawing.Image dwonImage = System.Drawing.Image.FromStream(imgRequest.GetResponse().GetResponseStream());
     if (!System.IO.Directory.Exists(newPath))//目标地址不存在自动创建
        {
            System.IO.Directory.CreateDirectory(newPath);
        }
     dwonImage.Save(newPath + ImsFileName);
     dwonImage.Dispose();
 }

 

展开阅读全文

没有更多推荐了,返回首页