Exemple #1
0
        /// <summary>
        /// 返回下载更新文件的临时目录
        /// </summary>
        public bool DownAutoUpdateFile(string downpath)
        {
            bool result = false;

            if (!System.IO.Directory.Exists(downpath))
            {
                System.IO.Directory.CreateDirectory(downpath);
            }
            string serverXmlFile = downpath + @"UpdateList.xml";


            WebRequest  req = null;
            WebResponse res = null;

            try
            {
                TempLog.Debug("下载更新配置文件UpdateList.xml,创建WebRequest");
                req = AppUpdater.Create(this.UpdaterUrl);
                TempLog.Debug("下载更新配置文件UpdateList.xml,完成WebRequest");
                res = req.GetResponse();
                TempLog.Debug("下载更新配置文件UpdateList.xml,完成WebRequest获取GetResponse");
                if (res.ContentLength > 0)
                {
                    WebClient wClient = AppUpdater.CreateClient();
                    TempLog.Debug("下载更新配置文件UpdateList.xml,开始下载");
                    wClient.DownloadFile(this.UpdaterUrl, serverXmlFile);
                    TempLog.Debug("下载更新配置文件UpdateList.xml,完成下载");
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (res != null)
                {
                    res.Close();
                    req.Abort();
                }
            }


            return(result);
        }
Exemple #2
0
        private void DownUpdateFile()
        {
            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;
                }
            }
            bool        isFinish = false;
            WebClient   wcClient = AppUpdater.CreateClient();
            WebResponse webRes   = null;
            WebRequest  webReq   = null;

            for (int i = 0; i < this.lvUpdateList.Items.Count; i++)
            {
                string UpdateFile = lvUpdateList.Items[i].Text.Trim();
                //string UpdateFile = WindowFormDelegate.GetListViewText(lvUpdateList, i);
                string updateFileUrl = updateUrl + UpdateFile;
                long   fileLength    = 0;
                WindowFormDelegate.SetMainThreadHint(lbFileListHint, string.Format("共{0}个文件待更新,准备更新第{1}个文件中", lvUpdateList.Items.Count.ToString(), (i + 1).ToString()));
                try
                {
                    TempLog.Debug(string.Format("下载更新文件{0},创建WebRequest", updateFileUrl));
                    webReq = AppUpdater.Create(updateFileUrl);
                    TempLog.Debug("完成创建WebRequest");
                    TempLog.Debug("下载更新文件,准备获取WebResponse");
                    webRes = webReq.GetResponse();
                    TempLog.Debug("下载更新文件,完成获取WebResponse");
                    fileLength = webRes.ContentLength;
                    WindowFormDelegate.SetMainThreadHint(this.lbState, "正在下载更新文件,请稍后...");
                    //lbState.Text = "正在下载更新文件,请稍后...";
                    pbDownFile.Value   = 0;
                    pbDownFile.Maximum = (int)fileLength;

                    TempLog.Debug("下载更新文件,获取GetResponseStream");
                    Stream srm = webRes.GetResponseStream();
                    TempLog.Debug("下载更新文件,完成获取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);
                    //  isFinish = false;
                    // continue;
                    Console.WriteLine("下载异常:" + ex.ToString());
                }
                finally
                {
                    if (webRes != null)
                    {
                        webRes.Close();
                        webReq.Abort();
                    }
                }
                if (i == this.lvUpdateList.Items.Count - 1)
                {
                    isFinish = true;
                    //this.btnFinish_Click(null, null);
                }
            }
            InvalidateControl();
            if (isFinish)
            {
                this.CopyFileThread();
            }
            //btnFinish_Click(null, null);
            else
            {
                this.StartMainApplication();
            }
        }
Exemple #3
0
        private void FrmUpdate_Load(object sender, System.EventArgs e)
        {
            panel2.Visible    = false;
            btnFinish.Visible = false;

            string localXmlFile  = Application.StartupPath + "\\UpdateList.xml";
            string serverXmlFile = string.Empty;


            try
            {
                //从本地读取更新配置文件信息
                TempLog.Debug("准备加载启动路径下的UpdateList.xml更新文件!");
                updaterXmlFiles = new XmlFiles(localXmlFile);
                mainAppExe      = updaterXmlFiles.GetNodeValue("//EntryPoint");
                TempLog.Debug("加载启动路径下的UpdateList.xml更新文件并读取更新完需执行的exe文件名完毕!");
            }
            catch (Exception ex)
            {
                TempLog.Info("加载UpdateList.xml出现异常:" + ex);
                //MessageBox.Show("配置文件出错!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                this.StartMainApplication();
                return;
            }
            //获取服务器地址
            updateUrl = updaterXmlFiles.GetNodeValue("//Url");

            AppUpdater appUpdater = new AppUpdater();

            appUpdater.UpdaterUrl = updateUrl + "UpdateList.xml";

            //与服务器连接,下载更新配置文件
            try
            {
                TempLog.Debug("准备下载更新配置文件列表UpdateList.xml");
                tempUpdatePath = Environment.GetEnvironmentVariable("Temp") + "\\" + "_" + updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value + "_" + "y" + "_" + "x" + "_" + "m" + "_" + "\\";
                appUpdater.DownAutoUpdateFile(tempUpdatePath);
                TempLog.Debug("完成下载更新配置文件列表UpdateList.xml");
            }
            catch (Exception ex)
            {
                TempLog.Info("下载更新配置文件列表UpdateList.xml出现异常:" + ex);
                //MessageBox.Show("与服务器连接失败,操作超时!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.StartMainApplication();
                return;
            }

            //获取更新文件列表
            Hashtable htUpdateFile = new Hashtable();

            serverXmlFile = tempUpdatePath + "\\UpdateList.xml";
            if (!File.Exists(serverXmlFile))
            {
                return;
            }

            availableUpdate = appUpdater.CheckForUpdate(serverXmlFile, localXmlFile, out htUpdateFile);
            if (availableUpdate > 0)
            {
                for (int i = 0; i < htUpdateFile.Count; i++)
                {
                    string [] fileArray = (string [])htUpdateFile[i];
                    lvUpdateList.Items.Add(new ListViewItem(fileArray));
                }
            }
            this.lbFileListHint.Text = string.Format("共{0}个文件待更新,准备更新中{1}", lvUpdateList.Items.Count.ToString(), string.Empty);
            btnNext_Click(null, null);
//			else
//				btnNext.Enabled = false;
        }