日期:2014-05-18 浏览次数:21189 次
private void ThreadFunction() { this.Cursor = Cursors.WaitCursor; mainAppExe = updaterXmlFiles.GetNodeValue("//EntryPoint"); Process[] allProcess = Process.GetProcesses(); foreach (Process p in allProcess) { if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower()) { for (int i = 0; i < p.Threads.Count; i++) p.Threads[i].Dispose(); p.Kill(); isRun = true; } } WebClient wcClient = new WebClient(); for (int i = 0; i < this.lvUpdateList.Items.Count; i++) { string UpdateFile = lvUpdateList.Items[i].Text.Trim(); string updateFileUrl = updateUrl + lvUpdateList.Items[i].Text.Trim(); long fileLength = 0; WebRequest webReq = WebRequest.Create(updateFileUrl); WebResponse webRes = webReq.GetResponse(); fileLength = webRes.ContentLength; lbState.Text = "正在下载更新文件,请稍后..."; pbDownFile.Value = 0; pbDownFile.Maximum = (int)fileLength; try { Stream srm = webRes.GetResponseStream(); StreamReader srmReader = new StreamReader(srm); byte[] bufferbyte = new byte[fileLength]; int allByte = (int)bufferbyte.Length; int startByte = 0; while (fileLength > 0) { Application.DoEvents(); int downByte = srm.Read(bufferbyte, startByte, allByte); if (downByte == 0) { break; }; startByte += downByte; allByte -= downByte; pbDownFile.Value += downByte; float part = (float)startByte / 1024; float total = (float)bufferbyte.Length / 1024; int percent = Convert.ToInt32((part / total) * 100); this.lvUpdateList.Items[i].SubItems[2].Text = percent.ToString() + "%"; } string tempPath = tempUpdatePath + UpdateFile; CreateDirtory(tempPath); FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write); fs.Write(bufferbyte, 0, bufferbyte.Length); srm.Close(); srmReader.Close(); fs.Close(); } catch (WebException ex) { MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } InvalidateControl(); this.Cursor = Cursors.Default; }