118 lines
3.7 KiB
C#
118 lines
3.7 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Drawing;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Management;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Windows.Forms;
|
|||
|
|
|||
|
namespace VersionUpdate
|
|||
|
{
|
|||
|
public partial class InstallFileForm : Form
|
|||
|
{
|
|||
|
List<FileInfo> infos;
|
|||
|
Client client;
|
|||
|
public InstallFileForm(List<FileInfo> infos, Client client)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
this.client = client;
|
|||
|
this.infos = infos;
|
|||
|
}
|
|||
|
|
|||
|
public void Kill(string filePath)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(filePath)) return;
|
|||
|
var wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process";
|
|||
|
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
|
|||
|
using (var results = searcher.Get())
|
|||
|
{
|
|||
|
var query = from p in Process.GetProcesses()
|
|||
|
join mo in results.Cast<ManagementObject>()
|
|||
|
on p.Id equals (int)(uint)mo["ProcessId"]
|
|||
|
select new
|
|||
|
{
|
|||
|
Process = p,
|
|||
|
Path = (string)mo["ExecutablePath"],
|
|||
|
CommandLine = (string)mo["CommandLine"],
|
|||
|
};
|
|||
|
|
|||
|
foreach (var item in query)
|
|||
|
{
|
|||
|
if (filePath == item.Path)
|
|||
|
{
|
|||
|
item.Process.Kill();
|
|||
|
item.Process.WaitForExit();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{ }
|
|||
|
}
|
|||
|
|
|||
|
private bool IsClose = false;
|
|||
|
private void InstallFileForm_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
var method = new Action(delegate ()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
int i = 1;
|
|||
|
Thread.Sleep(2000);
|
|||
|
|
|||
|
Kill(Program.ExeName);
|
|||
|
|
|||
|
foreach (var item in infos)
|
|||
|
{
|
|||
|
this.Invoke(new Action(delegate
|
|||
|
{
|
|||
|
this.label1.Text = $"正在安装第{i}个文件包...(共{infos.Count}个)";
|
|||
|
}));
|
|||
|
this.client.InstallFile(item);
|
|||
|
Application.DoEvents();
|
|||
|
}
|
|||
|
this.Invoke(new Action(delegate
|
|||
|
{
|
|||
|
this.label1.Text = $"安装完成!";
|
|||
|
IsClose = true;
|
|||
|
this.Close();
|
|||
|
}));
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.Invoke(new Action(delegate
|
|||
|
{
|
|||
|
this.label1.Text = $"安装失败:" + ex.Message;
|
|||
|
}));
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
IsClose = true;
|
|||
|
}
|
|||
|
});
|
|||
|
method.BeginInvoke(null, null);
|
|||
|
}
|
|||
|
|
|||
|
private void InstallFileForm_FormClosing(object sender, FormClosingEventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (!IsClose)
|
|||
|
{
|
|||
|
MessageBox.Show("安装还未结束,不能中途退出!", "友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
e.Cancel = true;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{ }
|
|||
|
}
|
|||
|
}
|
|||
|
}
|