额,太懒了 直接上示例代码。。。
/// <summary> /// 下载文件到本地 2017-05-31 /// </summary> /// <param name="DownloadPath">本地下载目录</param> /// <param name="FullFilePath">下载地址</param> /// <param name="FileName">文件名</param> /// <returns></returns> private void DownLoadSoft(string DownloadPath, string FullFilePath, string FileName) { ParameterizedThreadStart s = new ParameterizedThreadStart(DownLoadSoftAu); Thread thread = new Thread(s); thread.IsBackground = true; thread.Start(new object[] { DownloadPath, FullFilePath, FileName }); thread.Join(); } private void DownLoadSoftAu(object obj) { string DownloadPath = ((object[])obj)[0].ToString().Trim(); string FullFilePath =((object[])obj)[1].ToString().Trim(); string FileName = ((object[])obj)[2].ToString().Trim(); try { if (!Directory.Exists(DownloadPath)) { Directory.CreateDirectory(DownloadPath); } using (FileStream fs = new FileStream(DownloadPath + FileName, FileMode.Create, FileAccess.Write)) { //创建请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FullFilePath); //接收响应 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //输出流 Stream responseStream = response.GetResponseStream(); byte[] bufferBytes = new byte[10000];//缓冲字节数组 int bytesRead = -1; while ((bytesRead = responseStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0) { fs.Write(bufferBytes, 0, bytesRead); } //关闭写入 fs.Flush(); fs.Close(); } } catch (Exception exp) { //返回错误消息 } }