/// <summary> /// 检查更新 /// </summary> private void CheckDownlaod() { try { string systemVersion; systemVersion = RFVersionInfo.GetSystemVersion(); //检查新版本 if (_objCardTypeBLL.Programkeep(systemVersion, out URL)) { //说明有更新 if (!string.IsNullOrEmpty(URL)) { this.tbxUserName.Enabled = false; this.tbxPassWord.Enabled = false; this.btnLogin.Enabled = false; this.btnClose.Enabled = false; MessageBoxForm.Show("系统有新版本,稍后将进行系统更新!", MessageBoxButtons.OK); _request = (HttpWebRequest)HttpWebRequest.Create(URL); _request.BeginGetResponse(new AsyncCallback(ResponseReceived), null); } } } catch (Exception ex) { // MessageBoxForm.Show("连接不到网络,请检查网络连接!", MessageBoxButtons.OK); Log.WriterLine(ELevel.error, "检查更新", ex.Message); } }
string URL = string.Empty; //保存下载地址 /// <summary> /// 接收响应 /// </summary> /// <param name="res"></param> void ResponseReceived(IAsyncResult res) { try { _response = (HttpWebResponse)_request.EndGetResponse(res); } catch (WebException /*ex*/) { MessageBoxForm.Show("下载错误!", MessageBoxButtons.OK); return; } Cursor.Current = Cursors.WaitCursor; _dataBuffer = new byte[DataBlockSize]; _fs = new FileStream(RFVersionInfo.GetCurrentDirectory() + @"\" + "Update_CS.exe", FileMode.Create); _response.GetResponseStream().BeginRead(_dataBuffer, 0, DataBlockSize, new AsyncCallback(OnDataRead), this); Cursor.Current = Cursors.Default; }
/// <summary> /// 读取数据 /// </summary> /// <param name="res"></param> void OnDataRead(IAsyncResult res) { //结束读取 int nBytes = _response.GetResponseStream().EndRead(res); _fs.Write(_dataBuffer, 0, nBytes); if (nBytes > 0) { _response.GetResponseStream().BeginRead(_dataBuffer, 0, DataBlockSize, new AsyncCallback(OnDataRead), this); } else { _fs.Close(); _fs = null; Cursor.Current = Cursors.WaitCursor; int index = URL.LastIndexOf(@"/") > URL.LastIndexOf(@"\") ? URL.LastIndexOf(@"/") : URL.LastIndexOf(@"\"); //获得需要执行的程序 string execFileName = RFVersionInfo.GetCurrentDirectory() + @"\" + URL.Substring(index + 1); if (File.Exists(execFileName)) { //由进程重新打开 Process.Start(execFileName, null); Cursor.Current = Cursors.Default; Thread.Sleep(500); Process.GetCurrentProcess().Kill(); } else { //提示用户 MessageBoxForm.Show("未找到启动文件:" + execFileName + ",请联系管理员!", MessageBoxButtons.OK); } Application.Exit(); } }