Example #1
0
        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;
        }
Example #2
0
        /// <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();
            }
        }