using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
using Microsoft.Win32;
using System.Reflection;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using DevExpress.XtraEditors;
namespace DutySystem
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
#region 窗体事件
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VersionCheck(Application.StartupPath);
}
#endregion
#region 全局变量
byte[] IVArray ={ 57, 160, 21, 190, 162, 87, 36, 239 };
//加密使用iv
byte[] keyArray = { 32, 248, 65, 42, 22, 36, 63, 225, 22, 165, 157, 225, 177, 91, 243, 41, 67, 25, 39, 63, 229, 148, 163, 232 };
//服务器更新列表暂存地址
string ServerPath = Application.StartupPath + "//server.xml";
//本地配置文件
string MachineConfig = Application.StartupPath + "//MachineConfig.xml";
//本地版本文件
string LocalPath = Application.StartupPath + "//UpdateConfig.xml";
//开始下载
string Begin_DownLoad_FileName = "";
int Begin_DownLoad_Length = 0;
//下载中
string DownLoading_FileName = "";
int DownLoading_First = 0;
//单个文件下载成功
string Completed_DownLoad_FileName = "";
//开始安装事件
string Begin_Install_FileName = "";
int Begin_Install_Length = 0;
//单个文件安装完成
string Completed_File_Install_FileName = "";
#endregion
#region 公共方法
#region 升级版本 private void VersionCheck(string desPath)
/// <summary>
/// 从webServiceAddress中下载新版本
/// </summary>
/// <param name="desPath">本地文件升级目录</param>
private void VersionCheck(string desPath)
{
//注册监听服务端口
try
{
AsynCall updateVers = new AsynCall();
updateVers.LocalPath = LocalPath;
updateVers.MachineConfig = MachineConfig;
updateVers.ServerPath = ServerPath;
//开始下载事件
//updateVers.Begin_DownLoad += new AsynCall._Begin_DownLoad(updateVers_Begin_DownLoad);
//下载中事件
updateVers.DownLoading += new AsynCall._DownLoading(updateVers_DownLoading);
//所有文件下载成功事件
updateVers.Completed_DownLoad += new AsynCall._Completed_DownLoad(updateVers_Completed_DownLoad);
//开始安装事件
updateVers.Begin_Install += new AsynCall._Begin_Install(updateVers_Begin_Install);
//单个文件安装成功事件
updateVers.Completed_File_Install += new AsynCall._Completed_File_Install(updateVers_Completed_File_Install);
//更新完成事件
updateVers.RunCompleted += new AsynCall._RunCompleted(updateVers_RunCompleted);
//代理赋值
updateVers.ClientWebProxy = GetProxy();
//执行更新
//此步操作直接进行更新,不再判断版本
Lb_InstallStatus.Text = "正在获取服务器版本请稍等......";
string ver = updateVers.GetServerVer();
if (ver.Length > 2)
{
if (ver[0] == 'o' && ver[1] == 'k')
{
//正常格式 ok|本地版本|服务器版本|是否需要更新
this.Text = "当前版本:" + ver.Split('|')[1].ToString() + " 服务器版本:" + ver.Split('|')[2].ToString();
if (ver.Split('|')[3].ToString() == "true")
{
this.Text += "(您需要更新)";
//执行更新
updateVers.VersionUpdate(desPath);
}
else
{
this.Text += "(您不需要更新)";
Pb_Download.Value = 0;
Pb_Installer.Value = 0;
RunMainProcess();
return;
}
}
}
else
{
MessageBox.Show("版本文件获取失败,请检查代理设置!");
}
}
catch (Exception ex)
{
MessageBox.Show(this, "升级失败,原因是:" + ex.Message);
}
}
#region 升级相关事件
开始下载
//string Begin_DownLoad_FileName = "";
//int Begin_DownLoad_Length = 0;
下载中
//string DownLoading_FileName = "";
单个文件下载成功
//string Completed_DownLoad_FileName = "";
开始安装事件
//string Begin_Install_FileName = "";
//int Begin_Install_Length = 0;
单个文件安装完成
//string Completed_File_Install_FileName = "";
//更新完成后事件
string updateVers_RunCompleted()
{
string Olock = "";
lock (Olock)
{
MethodInvoker mi = new MethodInvoker(updateVers_RunCompleted_Enovi);
BeginInvoke(mi);
}
return "";
}
开始下载事件
//string updateVers_Begin_DownLoad(string fileName, int length)
//{
// string Olock = "";
// lock (Olock)
// {
// Begin_DownLoad_FileName = fileName;
// Begin_DownLoad_Length = length;
// MethodInvoker mi = new MethodInvoker(updateVers_Completed_DownLoad_Enovi);
// BeginInvoke(mi);
// }
// return "";
//}
//下载中事件
string updateVers_DownLoading(string fileName, int isFirst, int maxLength)
{
string Olock = "";
lock (Olock)
{
DownLoading_FileName = fileName;
DownLoading_First = isFirst;
Begin_DownLoad_Length = maxLength;
MethodInvoker mi = new MethodInvoker(updateVers_DownLoading_Enovi);
BeginInvoke(mi);
}
return "";
}
//单个文件下载成功事件
string updateVers_Completed_DownLoad(string fileName)
{
string Olock = "";
lock (Olock)
{
Completed_DownLoad_FileName = fileName;
MethodInvoker mi = new MethodInvoker(updateVers_Completed_DownLoad_Enovi);
BeginInvoke(mi);
}
return "";
}
//开始安装事件
string updateVers_Begin_Install(string fileName, int filesCount)
{
string Olock = "";
lock (Olock)
{
Begin_Install_FileName = fileName;
Begin_Install_Length = filesCount;
MethodInvoker mi = new MethodInvoker(updateVers_Begin_Install_Enovi);
BeginInvoke(mi);
}
return "";
}
//单个文件安装完成事件
string updateVers_Completed_File_Insta