private void btnCancel_Click(object sender, EventArgs e) { HttpDownloader.Stop(); isCanceled = true; if (threadStart != null) { threadStart.Abort(); } UpdateControls(false); TipRoute(); }
private void CheckConfigXML() { try { if (!Inite()) { UpdateControls(false); return; } string strTemp = DateTime.Now.Ticks.ToString(); string file = Application.StartupPath + "\\updateServerSide.xml"; string fileTemp = file + "." + strTemp + ".tmp"; string fileBak = file + "." + strTemp + ".bak"; CheckForIllegalCrossThreadCalls = false; HttpDownloader httpDown = new HttpDownloader(downTimeout); httpDown.Download(GetURL() + "/" + strUpdateXmlFile, fileTemp); if (File.Exists(file)) { Directory.Move(file, fileBak); } Directory.Move(fileTemp, file); } catch (ThreadAbortException) { //在“取消”按钮里面有后续处理 } catch (Exception ee) { Common.LogWrite(ee.ToString()); this.BeginInvoke(new MethodInvoker(delegate { if (!isCanceled) { MessageBox.Show("更新失败,请重试或取消。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } UpdateControls(false); TipRoute(); })); return; } threadStart = new System.Threading.Thread(new System.Threading.ThreadStart(CheckNewFiles)); threadStart.Start(); }
private void Download(object fileName) { string strFileName = fileName.ToString(); string strTemp = DateTime.Now.Ticks.ToString();//临时文件名 string tempName = ""; string bakName = ""; try { string locationPath = GetURL(); string location = locationPath + "/" + strFileName; tempName = strFileName + strTemp + ".tmp"; bakName = strFileName + strTemp + ".bak"; HttpDownloader ht = new HttpDownloader(downTimeout); ht.Download(location, tempName, this.pBar, labMes); try { File.Move(strFileName, bakName);//改名原文件 } catch { } File.Move(tempName, strFileName);//启用新文件 try { File.Delete(bakName);//删除原文件(测试发现:运行中的文件,可以被修改名字!但是不能被删除!) } catch { } } catch (ThreadAbortException) { //在“取消”按钮里面有后续处理 } catch (Exception e) { Common.LogWrite(strFileName + ":" + e.ToString()); if (!isCanceled) { this.Invoke(new MethodInvoker(delegate { MessageBox.Show(this, "更新失败,请重试或者取消更新。" + System.Environment.NewLine + System.Environment.NewLine + "文件名:" + strFileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); })); } this.BeginInvoke(new MethodInvoker(delegate { UpdateControls(false); TipRoute(); })); return; } finally { try { File.Delete(tempName); } catch { } } }