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

来源:互联网 发布:广州多益网络老板 编辑:程序博客网 时间:2024/06/05 02:39
 
//下载文件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;            }        }

0 0