首页

c# OAUS自动更新

c#

2020-9-10

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESBasic;
using AutoUpdater.Properties;
using ESBasic.Helpers;

namespace AutoUpdater
{
    /// <summary>
    /// 说明:
    /// OAUS使用的是免费版的通信框架ESFramework,最多支持10个人同时在线更新。如果要突破10人限制,请联系 www.oraycn.com
    /// </summary>
    public partial class MainForm : Form
    {
        private Updater updater;    
        private int fileCount = 0; //要升级的文件个数。
        private Timer timer = new Timer();
        private string callBackExeName;   //自动升级完成后,要启动的exe的名称。
        private string callBackPath = ""; //自动升级完成后,要启动的exe的完整路径。        
        private bool startAppAfterClose = false; //关闭升级窗体前,是否启动应用程序。


        public MainForm(string serverIP, int serverPort, string _callBackExeName, string title)
        {
            InitializeComponent();
            this.updater = new Updater(serverIP, serverPort);
            this.updater.ToBeUpdatedFilesCount  = new CbGeneric<int>(updater_ToBeUpdatedFilesCount);
            this.updater.UpdateStarted  = new CbGeneric(updater_UpdateStarted);
            this.updater.FileToBeUpdated  = new CbGeneric<int, string, ulong>(updater_FileToBeUpdated);
            this.updater.CurrentFileUpdatingProgress  = new CbGeneric<ulong, ulong>(updater_CurrentFileUpdatingProgress);
            this.updater.UpdateDisruptted  = new CbGeneric<string>(updater_UpdateDisruptted);
            this.updater.UpdateCompleted  = new CbGeneric(updater_UpdateCompleted);
            this.updater.ConnectionInterrupted  = new CbGeneric(updater_ConnectionInterrupted);
            this.updater.UpdateContinued  = new CbGeneric(updater_UpdateContinued);

            this.timer.Interval = 1000;
            this.timer.Tick  = new EventHandler(timer_Tick);
           
            DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);           
            this.callBackExeName = _callBackExeName;
            this.callBackPath = dir.Parent.FullName   "\\"   this.callBackExeName; //自动升级完成后,要启动的exe的完整路径。(1)被分发的程序的可执行文件exe必须位于部署目录的根目录。(2)OAUS的客户端(即整个AutoUpdater文件夹)也必须位于这个根目录。

            this.Text = title;
            this.label1.Text = Resources.InitialInformation;

            this.progressBar1.Visible = false;

            this.updater.Start();
                       
        }

        void updater_UpdateContinued()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new CbGeneric(this.updater_UpdateContinued));
            }
            else
            {
                //this.label_reconnect.Visible = false;
                this.label_reconnect.Text = "重连成功,正在续传...";
            }
        }

        void updater_ConnectionInterrupted()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new CbGeneric(this.updater_ConnectionInterrupted));
            }
            else
            {
                this.label_reconnect.Visible = true;
                this.label_reconnect.Text = "连接断开,正在重连中...";
            }
        }

        void updater_UpdateCompleted()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new CbGeneric(this.updater_UpdateCompleted));
            }
            else
            {
                this.label1.Text = Resources.UpdateSuccess;
                this.timer.Start();
            }
        }

        void updater_UpdateDisruptted(string disrupttedType)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<string>(updater_UpdateDisruptted), disrupttedType);
            }
            else
            {
                this.label1.Text = Resources.UpdateFailed;              
                this.label1.ForeColor = Color.Red;                
                MessageBox.Show(Resources.ConnectionInterrupted);
                this.startAppAfterClose = false;
                this.Close();
            }
        }

        void updater_CurrentFileUpdatingProgress(ulong total, ulong transfered)
        {
            this.SetProgress(total, transfered);
        }

        void updater_FileToBeUpdated(int fileIndex, string fileName, ulong fileSize)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<int, string, ulong>(updater_FileToBeUpdated), fileIndex, fileName, fileSize);
            }
            else
            {
                this.label1.Text = string.Format("{0}{1}{2}{3}{4}", Resources.Updateing_string1, this.fileCount, Resources.Updateing_string2, fileIndex   1, Resources.Updateing_string3);
                this.label2.Text = fileName;
            }
        }

        void updater_UpdateStarted()
        {
            this.ShowMessage(2);
        }

        void updater_ToBeUpdatedFilesCount(int needUpdatedFileCount)
        {
            this.fileCount = needUpdatedFileCount;
            if (needUpdatedFileCount == 0)
            {
                this.ShowMessage(1);
            }
        }      

        private void ShowMessage(int messageType)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new CbGeneric<int>(this.ShowMessage), messageType);
            }
            else
            {
                string message = "";
                if (messageType == 1)
                {
                    message = Resources.NoFileNeedUpdate;
                    this.timer.Start();
                }
                if (messageType == 2)
                {
                    message = string.Format("{0}{1}{2}", Resources.NeedUpdate_string1, this.fileCount, Resources.NeedUpdate_string2);
                    this.progressBar1.Visible = true;
                }
                if (messageType == 3)
                {
                    MessageBox.Show(Resources.ConnectionInterrupted);
                    this.startAppAfterClose = false;
                    this.Close();                    
                }

                this.label1.Text = message;
            }
        }       
       
        void timer_Tick(object sender, EventArgs e)
        {
            this.TimeUp();
        }

        private void TimeUp()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new CbGeneric(this.TimeUp));
            }
            else
            {
                this.startAppAfterClose = true;
                this.timer.Stop();
                this.Close();  
            }
        }       

        #region SetProgress
        private DateTime lastShowTime = DateTime.Now;
        /// <summary>
        /// 设置UI显示的进度表。
        /// </summary>       
        private void SetProgress(ulong total, ulong transmitted)
        {
            if (this.InvokeRequired)
            {
                object[] args = { total, transmitted };
                this.BeginInvoke(new CbGeneric<ulong, ulong>(this.SetProgress), args);
            }
            else
            {
                this.progressBar1.Maximum = 1000;
                this.progressBar1.Value = (int)(transmitted * 1000 / total);

                TimeSpan span = DateTime.Now - this.lastShowTime;
                if (span.TotalSeconds >= 1)
                {
                    this.lastShowTime = DateTime.Now;
                    this.label_progress.Text = string.Format("{0}/{1}", PublicHelper.GetSizeString(transmitted), PublicHelper.GetSizeString(total));
                }
            }
        }    
        #endregion     

        #region MainForm_FormClosing
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                string processName = this.callBackExeName.Substring(0, this.callBackExeName.Length - 4);
                ESBasic.Helpers.ApplicationHelper.ReleaseAppInstance(processName);

                if (!this.startAppAfterClose)
                {
                    return;
                }

                if (File.Exists(this.callBackPath))
                {
                    System.Diagnostics.Process myProcess = System.Diagnostics.Process.Start(this.callBackPath);                    
                }                
            }
            catch (Exception ee)
            {             
                MessageBox.Show(ee.Message);
            }
        }   
        #endregion              
    }
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
OAUS源码/32.ico , 4286
OAUS源码/AutoUpdater/32.ico , 4286
OAUS源码/AutoUpdater/App.config , 903
OAUS源码/AutoUpdater/AutoUpdater.csproj , 4733
OAUS源码/AutoUpdater/AutoUpdater.csproj.user , 369
OAUS源码/AutoUpdater/bin/Debug/AutoUpdater.exe , 36864
OAUS源码/AutoUpdater/bin/Debug/AutoUpdater.exe.config , 903
OAUS源码/AutoUpdater/bin/Debug/AutoUpdater.pdb , 42496
OAUS源码/AutoUpdater/bin/Debug/AutoUpdater.vshost.exe , 11608
OAUS源码/AutoUpdater/bin/Debug/AutoUpdater.vshost.exe.config , 903
OAUS源码/AutoUpdater/bin/Debug/AutoUpdater.vshost.exe.manifest , 2409
OAUS源码/AutoUpdater/bin/Debug/ESBasic.dll , 446976
OAUS源码/AutoUpdater/bin/Debug/ESBasic.xml , 215220
OAUSԴ￾/AutoUpdater/bin/Debug/ESFramework.dll , 1164800
OAUS源码/AutoUpdater/bin/Debug/ESFramework.xml , 354881
OAUS源码/AutoUpdater/bin/Debug/OAUS.Core.dll , 7680
OAUS源码/AutoUpdater/bin/Debug/OAUS.Core.pdb , 28160
OAUS源码/AutoUpdater/bin/Debug/UpdateConfiguration.xml , 344
OAUS源码/AutoUpdater/MainForm.cs , 9154
OAUS源码/AutoUpdater/MainForm.Designer.cs , 5309
OAUS源码/AutoUpdater/MainForm.resx , 12549
OAUS源码/AutoUpdater/obj/x86/Debug/AutoUpdater.csproj.FileListAbsolute.txt , 5120
OAUS源码/AutoUpdater/obj/x86/Debug/AutoUpdater.csproj.GenerateResource.Cache , 978
OAUS源码/AutoUpdater/obj/x86/Debug/AutoUpdater.csprojResolveAssemblyReference.cache , 19500
OAUS源码/AutoUpdater/obj/x86/Debug/AutoUpdater.exe , 36864
OAUS源码/AutoUpdater/obj/x86/Debug/AutoUpdater.MainForm.resources , 4852
OAUS源码/AutoUpdater/obj/x86/Debug/AutoUpdater.pdb , 42496
OAUS源码/AutoUpdater/obj/x86/Debug/AutoUpdater.Properties.Resources.resources , 1064
OAUS源码/AutoUpdater/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache , 1229
OAUS源码/AutoUpdater/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 5767
OAUS源码/AutoUpdater/obj/x86/Debug/ResolveAssemblyReference.cache , 14818
OAUS源码/AutoUpdater/obj/x86/Debug/TempPE/Properties.Resources.Designer.cs.dll , 5632
OAUS源码/AutoUpdater/obj/x86/Debug/TempPE/Properties.Resources.en-US.Designer.cs.dll , 3072
OAUS源码/AutoUpdater/Program.cs , 1924
OAUS源码/AutoUpdater/Properties/app.manifest , 2409
OAUS源码/AutoUpdater/Properties/AssemblyInfo.cs , 1439
OAUS源码/AutoUpdater/Properties/Resources.Designer.cs , 6718
OAUS源码/AutoUpdater/Properties/Resources.en-US.Designer.cs , 0
OAUS源码/AutoUpdater/Properties/Resources.en-US.resx , 7192
OAUS源码/AutoUpdater/Properties/Resources.resx , 7180
OAUS源码/AutoUpdater/Updater.cs , 15923
OAUS源码/OAUS.Core/bin/Debug/AutoUpdaterSystem.Core.pdb , 28160
OAUS源码/OAUS.Core/bin/Debug/AutoUpgrade.Core.pdb , 22016
OAUS源码/OAUS.Core/bin/Debug/ESBasic.dll , 446976
OAUS源码/OAUS.Core/bin/Debug/ESBasic.xml , 215220
OAUS源码/OAUS.Core/bin/Debug/OAUS.Core.dll , 7680
OAUS源码/OAUS.Core/bin/Debug/OAUS.Core.pdb , 28160
OAUS源码/OAUS.Core/Contract/DownloadFilesContract.cs , 322
OAUS源码/OAUS.Core/Contract/FilesInfoContract.cs , 388
OAUS源码/OAUS.Core/Contract/LastUpdateTimeContract.cs , 586
OAUS源码/OAUS.Core/FileUnit.cs , 1945
OAUS源码/OAUS.Core/InformationTypes.cs , 635
OAUS源码/OAUS.Core/IOausService.cs , 184
OAUS源码/OAUS.Core/OAUS.Core.csproj , 2882
OAUS源码/OAUS.Core/OAUS.Core.csproj.user , 227
OAUS源码/OAUS.Core/obj/Debug/AutoUpdaterSystem.Core.pdb , 28160
OAUS源码/OAUS.Core/obj/Debug/AutoUpgrade.Core.pdb , 22016
OAUS源码/OAUS.Core/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 5107
OAUS源码/OAUS.Core/obj/Debug/OAUS.Core.csproj.FileListAbsolute.txt , 3940
OAUS源码/OAUS.Core/obj/Debug/OAUS.Core.csprojResolveAssemblyReference.cache , 11131
OAUS源码/OAUS.Core/obj/Debug/OAUS.Core.dll , 7680
OAUS源码/OAUS.Core/obj/Debug/OAUS.Core.pdb , 28160
OAUS源码/OAUS.Core/Properties/AssemblyInfo.cs , 1431
OAUS源码/OAUS.Core/UpdateConfiguration.cs , 1104
OAUS源码/OAUS.Core/VersionHelper.cs , 1876
OAUS源码/OAUS.Server/32.ico , 4286
OAUS源码/OAUS.Server/AddFileVersionForm.cs , 3159
OAUS源码/OAUS.Server/AddFileVersionForm.Designer.cs , 5079
OAUS源码/OAUS.Server/AddFileVersionForm.resx , 12549
OAUS源码/OAUS.Server/App.config , 499
OAUS源码/OAUS.Server/bin/Debug/ESBasic.dll , 452096
OAUS源码/OAUS.Server/bin/Debug/ESBasic.xml , 215220
OAUSԴ￾/OAUS.Server/bin/Debug/ESFramework.dll , 1164800
OAUS源码/OAUS.Server/bin/Debug/ESFramework.xml , 354881
OAUS源码/OAUS.Server/bin/Debug/OAUS.Core.dll , 7680
OAUS源码/OAUS.Server/bin/Debug/OAUS.Core.pdb , 28160
OAUS源码/OAUS.Server/bin/Debug/OAUS.Server.exe , 74752
OAUS源码/OAUS.Server/bin/Debug/OAUS.Server.exe.config , 499
OAUS源码/OAUS.Server/bin/Debug/OAUS.Server.pdb , 46592
OAUS源码/OAUS.Server/bin/Debug/OAUS.Server.vshost.exe , 11608
OAUS源码/OAUS.Server/bin/Debug/OAUS.Server.vshost.exe.config , 499
OAUS源码/OAUS.Server/bin/Debug/OAUS.Server.vshost.exe.manifest , 490
OAUS源码/OAUS.Server/bin/Debug/UpdateConfiguration.xml , 323
OAUS源码/OAUS.Server/CustomizeHandler.cs , 2684
OAUS源码/OAUS.Server/FileVersionForm.cs , 7480
OAUS源码/OAUS.Server/FileVersionForm.Designer.cs , 11521
OAUS源码/OAUS.Server/FileVersionForm.resx , 13135
OAUS源码/OAUS.Server/MainForm.cs , 2262
OAUS源码/OAUS.Server/MainForm.Designer.cs , 7047
OAUS源码/OAUS.Server/MainForm.resx , 57775
OAUS源码/OAUS.Server/OAUS.Server.csproj , 5641
OAUS源码/OAUS.Server/OAUS.Server.csproj.user , 227
OAUS源码/OAUS.Server/OausService.cs , 603
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpdaterSystem.Server.exe , 35840
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpdaterSystem.Server.pdb , 38400
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpdaterSystem.Server.Properties.Resources.resources , 180
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpgradeServer.AddConfigFileForm.resources , 180
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpgradeServer.ConfigFilesForm.resources , 180
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpgradeServer.exe , 18944
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpgradeServer.pdb , 34304
OAUS源码/OAUS.Server/obj/x86/Debug/AutoUpgradeServer.Properties.Resources.resources , 180
OAUS源码/OAUS.Server/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache , 13606
OAUS源码/OAUS.Server/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6055
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.AddFileVersionForm.resources , 4852
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.csproj.FileListAbsolute.txt , 10113
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.csproj.GenerateResource.Cache , 1117
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.csprojResolveAssemblyReference.cache , 19757
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.exe , 74752
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.FileVersionForm.resources , 4852
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.MainForm.resources , 34784
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.pdb , 46592
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.Properties.Resources.resources , 180
OAUS源码/OAUS.Server/obj/x86/Debug/OAUS.Server.ServerForm.resources , 34784
OAUS源码/OAUS.Server/obj/x86/Debug/TempPE/Properties.Resources.Designer.cs.dll , 4608
OAUS源码/OAUS.Server/Program.cs , 3231
OAUS源码/OAUS.Server/Properties/AssemblyInfo.cs , 1420
OAUS源码/OAUS.Server/Properties/DataSources/ConfigObject.datasource , 597
OAUS源码/OAUS.Server/Properties/DataSources/FileObject.datasource , 593
OAUS源码/OAUS.Server/Properties/Resources.Designer.cs , 2862
OAUS源码/OAUS.Server/Properties/Resources.resx , 5612
OAUS源码/OAUS.Server/Properties/Settings.Designer.cs , 1111
OAUS源码/OAUS.Server/Properties/Settings.settings , 249
OAUS源码/OAUS.sln , 3523
OAUS源码/OAUS.suo , 136704
OAUS源码/Resources/ESBasic.dll , 452096
OAUS源码/Resources/ESBasic.xml , 215220
OAUSԴ￾/Resources/ESFramework.dll , 1164800
OAUS源码/Resources/ESFramework.xml , 354881
没有账号? 忘记密码?

社交账号快速登录