Ejemplo n.º 1
0
        private void downloadFiles()
        {
            GetNewFileList(Path.Combine(Application.StartupPath, "updateServerSide.xml"));

            if (arrayFiles.Count > 0)
            {
                //if (DialogResult.Yes == MessageBox.Show(this, "有" + m_ary.Count + "个文件可以更新!是否立即更新?", "提示", MessageBoxButtons.YesNo))
                {
                    string locationPath = GetURL();
                    string strTemp      = DateTime.Now.Ticks.ToString();//临时文件名
                    string tempName     = "";
                    string bakName      = "";
                    bool   bUpdateSucc  = true;
                    //pBar.Minimum = 0;
                    //pBar.Maximum = 100;// m_ary.Count + 1;

                    for (int i = 0; i < arrayFiles.Count; i++)
                    {
                        //pBar.Value = 0;
                        string[] strAry      = (string[])arrayFiles[i];
                        string   strFileName = strAry[0].Trim();
                        //labMes.Text = strFileName;//该语句会导致子线程更新主界面卡死!应使用Invoke操作

                        try
                        {
                            string location = locationPath + "/" + strFileName;
                            tempName = strFileName + strTemp + ".tmp";
                            bakName  = strFileName + strTemp + ".bak";

                            HttpDownloader ht = new HttpDownloader(downTimeout);
                            ht.Download(location, tempName, null, null);

                            //btnCancel.Enabled = false;
                            try
                            {
                                File.Move(strFileName, bakName);//改名原文件
                            }
                            catch (Exception ee) { EagleFileIO.LogWrite(ee.ToString() + strFileName); }

                            File.Move(tempName, strFileName);//启用新文件

                            try
                            {
                                File.Delete(bakName);//删除原文件(测试发现:运行中的文件,可以被修改名字!但是不能被删除!)
                            }
                            catch { }

                            //btnCancel.Enabled = true;
                        }
                        catch (Exception e)
                        {
                            EagleFileIO.LogWrite("更新失败:" + strFileName);
                            EagleFileIO.LogWrite(e.ToString());
                            //if (!isCanceled)
                            //    MessageBox.Show(this, "更新失败!请重试或者取消更新。"
                            //        + System.Environment.NewLine + System.Environment.NewLine + "文件名:" + strFileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            bUpdateSucc = false;
                            //tryCount = 2;
                        }
                        finally
                        {
                            try
                            {
                                File.Delete(tempName);
                            }
                            catch { }
                        }

                        if (!bUpdateSucc)
                        {
                            //btnUpdate.Enabled = true;
                            //picLoading.Visible = false;
                            break;
                        }
                        //System.Threading.Thread.Sleep(5000);
                    }

                    if (bUpdateSucc)
                    {
                        System.IO.File.Copy(Application.StartupPath + "\\updateServerSide.xml", Path.Combine(Application.StartupPath, strUpdateXmlFile), true);
                        System.IO.File.Delete(Application.StartupPath + "\\updateServerSide.xml");

                        EagleFileIO.LogWrite("自动更新成功!文件数:" + arrayFiles.Count);
                        //tryCount = 2;
                        return;
                    }
                    else
                    {
                        //MessageBox.Show("更新失败");
                    }
                }
                //else
                //{
                //    tryCount = 2;
                //    Application.Exit();
                //}
            }
            else
            {
                //MessageBox.Show("没有代码需要更新");
                //tryCount = 2;
                return;;
            }
        }