C#实现在winfrom程序中下载文件

 
//下载文件1
        public static void downfile(string downloadUrl,string filename,string filepath)
        {
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downloadUrl);
            hwr.Timeout = 15000;
            HttpWebResponse hwp = (HttpWebResponse)hwr.GetResponse();
            Stream ss = hwp.GetResponseStream();
            byte[] buffer = new byte[10240];
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }
            FileStream fs = new FileStream(
                string.Format(filepath + @"\" + filename),
                FileMode.Create);
            try
            {
                int i;
                while ((i = ss.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, i);
                }
                fs.Close();
                ss.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页