2025-03-28 09:49:56 +08:00

359 lines
14 KiB
C#

using SuperMap.RealEstate.Launcher;
using SuperMap.RealEstate.Launcher_WinForm;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace Launcher.Install
{
public partial class frmMain : Form
{
string _TempPath, _TempGuidPath, _InstallPath;
string _TempFile= "Config.ini";
public frmMain(string iniFilePath)
{
_TempPath = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "..\\Temp\\");
_TempGuidPath = _TempPath + Guid.NewGuid().ToString() + "\\";
_InstallPath = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "..\\Install\\");
LoadingHelper.Init(_TempPath);
if (!Directory.Exists(_TempGuidPath))
Directory.CreateDirectory(_TempGuidPath);
_TempFile = _TempGuidPath + LaunchHelper.Installer_IniFileName;
if (!string.IsNullOrEmpty(iniFilePath))
File.Move(iniFilePath, _TempFile);
InitializeComponent();
this.Text += Application.ProductVersion;
this.Width = 700;
_BackgroundDownload = new BackgroundDownload();
//_BackgroundDownload.
//_BackgroundDownload.ShowTimeOut = 1;
//_BackgroundDownload.
//_BackgroundDownload.ProgressBarVisible1 = true;
Compress7z.SeventZPrcessPath = "..\\Bin";
}
private void frmMain_Load(object sender, EventArgs e)
{
DataGridView_Initialize();
DataGridView_LoadData();
//LoadingHelper.SetProgress1Value(50);
LoadingHelper.Close(1);
}
#region DataGridView_Initialize
private void DataGridView_Initialize()
{
int index = 0;
dataGridView1.Columns[index++].HeaderText = "选择";//0
dataGridView1.Columns[index++].HeaderText = "分组";//1
dataGridView1.Columns[index++].HeaderText = "名字";//2
dataGridView1.Columns[index++].HeaderText = "描述";//3
dataGridView1.Columns[index++].HeaderText = "目录";//4
dataGridView1.Columns[index++].HeaderText = "执行文件";//5
dataGridView1.Columns[index++].HeaderText = "版本";//6
dataGridView1.Columns[index++].HeaderText = "部署位置";//7
dataGridView1.Columns[index++].HeaderText = "平台";//8
dataGridView1.Columns[index++].HeaderText = "参数";//9
dataGridView1.Columns[index++].HeaderText = "进度";//10
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
dataGridView1.Columns[i].Visible = false;
dataGridView1.Columns[i].ReadOnly = true;
}
dataGridView1.Columns[0].ReadOnly = false;
dataGridView1.Columns[0].Visible = true;
dataGridView1.Columns[3].Visible = true;
dataGridView1.Columns[6].Visible = true;
dataGridView1.Columns[8].Visible = true;
dataGridView1.Columns[8].DisplayIndex = 6;
dataGridView1.Columns[10].Visible = true;
dataGridView1.Columns[10].DisplayIndex = 1;
}
#endregion
#region DataGridView_LoadData
private void DataGridView_LoadData()
{
using (StreamReader _StreamReader = new StreamReader(_TempFile))
{
string[] _RowValues;
string _LineText;
while (!_StreamReader.EndOfStream)
{
_RowValues = new string[dataGridView1.Columns.Count];
_RowValues[0] = "true";
for (int i = 1; i < dataGridView1.Columns.Count - 1; i++)
{
_LineText = _StreamReader.ReadLine();
_RowValues[i] = _LineText.Substring(_LineText.IndexOf(":") + 1);
}
dataGridView1.Rows.Add(_RowValues);
_StreamReader.ReadLine();
}
_StreamReader.Close();
}
DadaGridView_AutoResizeColumn(3);
foreach (DataGridViewRow _Row in dataGridView1.Rows)
{
if (!System.Environment.Is64BitOperatingSystem)
{
if (_Row.Cells[8].Value.ToString().Equals("x64", StringComparison.CurrentCultureIgnoreCase))
{
_Row.Visible = false;
_Row.Cells[0].Value = "false";
}
}
_Row.Cells[10].Value = "等待下载";
if (File.Exists(_InstallPath + _Row.Cells[5].Value.ToString()) && File.Exists(_InstallPath + _Row.Cells[4].Value.ToString() + "\\Version.txt"))
{
StreamReader _StreamReader = File.OpenText(_InstallPath + _Row.Cells[4].Value.ToString() + "\\Version.txt");
if (_StreamReader.ReadToEnd() == _Row.Cells[6].Value.ToString())
_Row.Cells[10].Value = "等待安装";
_StreamReader.Close();
}
}
}
#endregion
#region DadaGridView_AutoResizeColumn
private void DadaGridView_AutoResizeColumn(int FillIndex)
{
for (int j = 0; j < 5; j++)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (i != FillIndex)
{
dataGridView1.AutoResizeColumn(i);
}
}
}
}
#endregion
BackgroundDownload _BackgroundDownload;
private void buttonInstall_Click(object sender, EventArgs e)
{
int _DownLoadCount = DataGridView_InitDownLoad();
if (_DownLoadCount > 0)
{
_BackgroundDownload.Progress2Value = 0;
_BackgroundDownload.SetProgress2Maximum(_DownLoadCount);
_BackgroundDownload.Progress1Value = 0;
}
string _7zFileName, _7zUnZipPath;
//下载
foreach (DataGridViewRow _Row in dataGridView1.Rows)
{
if (_Row.Visible == false)
continue;
_7zFileName = _Row.Cells[7].Value.ToString();
_7zFileName = _7zFileName.Substring(_7zFileName.LastIndexOf("/") + 1).Replace(".7z.Deploy", ".7z");
_7zFileName = _TempGuidPath + _Row.Cells[4].Value.ToString() + "\\" + _7zFileName;
_7zUnZipPath = _InstallPath + _Row.Cells[4].Value.ToString() + "\\";
if (_Row.Cells[10].Value.ToString() == "等待下载")
{
//_BackgroundDownload.Progress1Value = 0;
_BackgroundDownload.ProgressBarText = "下载 " + _Row.Cells[3].Value.ToString();
_BackgroundDownload.DownloadFileAsync(_Row.Cells[7].Value.ToString(), _7zFileName);
_BackgroundDownload.WaitingForDownloadFileAsyncCompleted();
_BackgroundDownload.Progress1Value = 100;
_Row.Cells[10].Value = "下载完成";
_BackgroundDownload.Progress2Value++;
}
}
//解压缩
foreach (DataGridViewRow _Row in dataGridView1.Rows)
{
if (_Row.Visible == false)
continue;
_7zFileName = _Row.Cells[7].Value.ToString();
_7zFileName = _7zFileName.Substring(_7zFileName.LastIndexOf("/") + 1).Replace(".7z.Deploy", ".7z");
_7zFileName = _TempGuidPath + _Row.Cells[4].Value.ToString() + "\\" + _7zFileName;
_7zUnZipPath = _InstallPath + _Row.Cells[4].Value.ToString() + "\\";
if (_Row.Cells[10].Value.ToString() == "下载完成")
{
if (File.Exists(_7zFileName))
{
_BackgroundDownload.ProgressBarText = "解压 " + _Row.Cells[3].Value.ToString();
if (Compress7z.UnZip(_7zFileName, _7zUnZipPath))
{
File.Delete(_7zFileName);
using (StreamWriter _StreamWriter = File.CreateText(_7zUnZipPath + "Version.txt"))
{
_StreamWriter.Write(_Row.Cells[6].Value.ToString());
_StreamWriter.Close();
}
_Row.Cells[10].Value = "等待安装";
}
else
{
File.Delete(_7zFileName);
_Row.Cells[10].Value = "解压失败";
_BackgroundDownload.ProgressBarText = "解压 失败!";
}
//MessageBox.Show("A");
}
else
{
_Row.Cells[10].Value = "等待下载";
}
}
//_BackgroundDownload.Progress2Value++;
}
//安装
if (checkBoxStartSetup.Checked)
{
foreach (DataGridViewRow _Row in dataGridView1.Rows)
{
if (_Row.Visible == false)
continue;
if (_Row.Cells[10].Value.ToString() != "等待安装")
continue;
_BackgroundDownload.ProgressBarText = "安装 " + _Row.Cells[3].Value.ToString();
if (SetupPrcess(_InstallPath + _Row.Cells[5].Value.ToString(),
checkBoxParameters.Checked ? "" : _Row.Cells[9].Value.ToString()))
{
//_BackgroundDownload.ProgressBarText = "成功!";
_Row.Cells[10].Value = "安装成功";
}
else
_BackgroundDownload.ProgressBarText = "安装 失败!";
//MessageBox.Show("A");
}
}
_BackgroundDownload.ProgressBarHide();
}
private int DataGridView_InitDownLoad()
{
int _DownLoadCount = 0;
foreach (DataGridViewRow _Row in dataGridView1.Rows)
{
if (!bool.Parse(_Row.Cells[0].Value.ToString()))
_Row.Visible = false;
else
{
if (_Row.Cells[10].Value == null)
_Row.Cells[10].Value = "等待下载";
if (_Row.Cells[10].Value.ToString().EndsWith("失败"))
_Row.Cells[10].Value = "等待下载";
if (_Row.Cells[10].Value.ToString() == "等待下载")
_DownLoadCount++;
}
}
return _DownLoadCount;
}
private bool SetupPrcess(string fileName, string arguments)
{
Process _SetupPrcess = new System.Diagnostics.Process();
_SetupPrcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
_SetupPrcess.StartInfo.FileName = fileName;
_SetupPrcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(fileName);
_SetupPrcess.StartInfo.CreateNoWindow = false;
_SetupPrcess.StartInfo.Arguments = arguments;
_SetupPrcess.Start();
_SetupPrcess.WaitForExit();
if (_SetupPrcess.HasExited)
{
int iExitCode = _SetupPrcess.ExitCode;
_SetupPrcess.Close();
if (iExitCode != 0 && iExitCode != 1)
{
return false;
}
return true;
}
MessageBox.Show(_SetupPrcess.ExitCode.ToString());
_SetupPrcess.Close();
return true;
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
_BackgroundDownload.Close();
try
{
if (Directory.Exists(_TempGuidPath))
Directory.Delete(_TempGuidPath, true);
}
catch
{
}
}
//private void dataGridView1_DoubleClick(object sender, EventArgs e)
//{
// if (dataGridView1.SelectedRows.Count == 0)
// {
// MessageBox.Show("请在安装列表中选择要安装的项!", "驿商科技", MessageBoxButtons.OK,
// MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
// return;
// }
// NewMethod(dataGridView1.SelectedRows[0]);
//}
private void buttonSetup_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("请在安装列表中选择要安装的项!", "驿商科技", MessageBoxButtons.OK,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
return;
}
NewMethod(dataGridView1.SelectedRows[0]);
}
private void NewMethod(DataGridViewRow _Row)
{
if (_Row.Cells[10].Value.ToString() != "等待安装")
{
MessageBox.Show("请在安装列表中选择“等待安装”的项!", "驿商科技", MessageBoxButtons.OK,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
return;
}
if (SetupPrcess(_InstallPath + _Row.Cells[5].Value.ToString(), ""))
{
_Row.Cells[10].Value = "安装成功";
}
_Row.Selected = false;
}
private void checkBoxStartSetup_Click(object sender, EventArgs e)
{
checkBoxStartSetup.Visible = false;
buttonSetup.Visible = true;
checkBoxParameters.Visible = false;
buttonInstall.Text = "下 载(&D)";
dataGridView1.ClearSelection();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
return;
NewMethod(dataGridView1.Rows[e.RowIndex]);
}
}
}