/// <summary> /// HTTP下载远程文件并保存本地的函数 /// </summary> void StartDownLoad(SynFileInfo FileInfo) { try { FileInfo.LastTime = DateTime.Now; //再次new 避免WebClient不能I/O并发 WebClient client = new WebClient(); //异步下载 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); var savePath = System.AppDomain.CurrentDomain.BaseDirectory; if (!Directory.Exists(temp))//如果不存在就创建file文件夹 { Directory.CreateDirectory(temp); } string v = temp + FileInfo.DocName.Replace("/", "\\"); string newPath = v.Substring(0, v.LastIndexOf('\\')); if (!Directory.Exists(newPath))//如果不存在就创建file文件夹 { Directory.CreateDirectory(newPath); } string file = this.infor.Server + FileInfo.DocName.Replace("\\", "/"); client.DownloadFileAsync(new Uri(file), v, FileInfo); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 下载完成调用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { //到此则一个文件下载完毕 DownloadCompliteFileInfo = (SynFileInfo)e.UserState; if (DownloadCompliteFileInfo.FileSize != DownloadCompliteFileInfo.Size) { DownloadCompliteFileInfo.RowObject.Cells["Image"].Value = Resources.cross; DownloadCompliteFileInfo.RowObject.Cells["SynSpeed"].Value = "0%"; ECON++; this.DownloadSize = this.DownloadSize - DownloadCompliteFileInfo.FileSize; } else { this.infor.DownloadFileList.Remove(this.infor.DownloadFileList.Where(c => c.Name == DownloadCompliteFileInfo.DocName).FirstOrDefault()); DownloadCompliteFileInfo.RowObject.Cells["Image"].Value = Resources.tick; CON++; lbl_SUM.Text = string.Format("文件:{0}/{1}", CON, SUM); string path = "//File[@Name='" + DownloadCompliteFileInfo.DocName + "']"; XmlNode root = file[2].SelectSingleNode(path); if (root == null) { XmlElement subElement = localDoc.CreateElement("File"); subElement.SetAttribute("Name", DownloadCompliteFileInfo.DocName); subElement.SetAttribute("Version", DownloadCompliteFileInfo.Version); subElement.SetAttribute("Size", DownloadCompliteFileInfo.Size.ToString()); file[2].AppendChild(subElement); } else { root.Attributes[1].Value = DownloadCompliteFileInfo.Version; root.Attributes[2].Value = DownloadCompliteFileInfo.Size.ToString(); } localDoc.Save(xmlPath); } m_SynFileInfoList.Remove(DownloadCompliteFileInfo); if (m_SynFileInfoList.Count <= 0) { tr.Abort(); pb_All.Value = (int)this.DownloadSize; lbl_Size.Text = string.Format("大小:{0}/{1}", FileOperate.GetAutoSizeString(this.DownloadSize, 2), TotalSize); if (ECON != 0) { lbl_Error.Text = "文件更新失败" + ECON + "个,请尝试重新更新!\n提示:本地网络限制或访问服务器会更新失败"; lbl_Error.Visible = true; btn_OK.Text = "重新更新"; } else { CutNewFile(); CreateDesk(); CompledUpdate(); } file[0].InnerText = this.version; file[1].InnerText = this.remark; localDoc.Save(xmlPath); btn_OK.Enabled = true; } }
/// <summary> /// 下载完成调用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { //到此则一个文件下载完毕 SynFileInfo m_SynFileInfo = (SynFileInfo)e.UserState; m_SynFileInfoList.Remove(m_SynFileInfo); //if (m_SynFileInfoList.Count <= 0) //{ //} }
/// <summary> /// 下载完成调用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { //到此则一个文件下载完毕 SynFileInfo m_SynFileInfo = (SynFileInfo)e.UserState; m_SynFileInfoList.Remove(m_SynFileInfo); if (m_SynFileInfoList.Count <= 0) { //此时所有文件下载完毕 btnStartDownLoad.Enabled = true; } }
/// <summary> /// 下载进度条 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { SynFileInfo m_SynFileInfo = (SynFileInfo)e.UserState; m_SynFileInfo.SynProgress = e.ProgressPercentage + "%"; double secondCount = (DateTime.Now - m_SynFileInfo.LastTime).TotalSeconds; m_SynFileInfo.SynSpeed = FileOperate.GetAutoSizeString(Convert.ToDouble(e.BytesReceived / secondCount), 2) + "/s"; //更新DataGridView中相应数据显示下载进度 m_SynFileInfo.RowObject.Cells["SynProgress"].Value = m_SynFileInfo.SynProgress; //更新DataGridView中相应数据显示下载速度(总进度的平均速度) m_SynFileInfo.RowObject.Cells["SynSpeed"].Value = m_SynFileInfo.SynSpeed; }
/// <summary> /// 下载进度条 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { DownloadFileInfo = (SynFileInfo)e.UserState; DownloadFileInfo.RowObject.Cells["Image"].Value = Resources.hourglass_go; DownloadFileInfo.SynProgress = e.ProgressPercentage + "%"; DownloadFileInfo.SynSpeed = FileOperate.GetAutoSizeString(e.BytesReceived, 2); DownloadFileInfo.RowObject.Cells["SynSpeed"].Value = DownloadFileInfo.SynProgress; DownloadFileInfo.FileSize = e.TotalBytesToReceive; this.DownloadSize += ToInt(e.BytesReceived) - ToInt(DownloadFileInfo.RowObject.Cells["SynProgress"].Value); DownloadFileInfo.RowObject.Cells["SynProgress"].Value = e.BytesReceived; pb_All.Value = (int)this.DownloadSize; lbl_Size.Text = string.Format("大小:{0}/{1}", FileOperate.GetAutoSizeString(this.DownloadSize, 2), TotalSize); }
/// <summary> /// HTTP下载远程文件并保存本地的函数 /// </summary> void StartDownLoad(object o) { SynFileInfo m_SynFileInfo = (SynFileInfo)o; m_SynFileInfo.LastTime = DateTime.Now; //再次new 避免WebClient不能I/O并发 WebClient client = new WebClient(); if (m_SynFileInfo.Async) { //异步下载 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); client.DownloadFileAsync(new Uri(m_SynFileInfo.DownPath), m_SynFileInfo.SavePath, m_SynFileInfo); } else { client.DownloadFile(new Uri(m_SynFileInfo.DownPath), m_SynFileInfo.SavePath); } }