old_flsystem/类库/Update/InstallFileForm.cs

146 lines
5.0 KiB
C#
Raw Permalink Normal View History

2022-09-20 03:10:29 +00:00
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 OnlineUpdate
{
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);
//for (int z = 0; z < 4; z++)
//{
// try
// {
// Process[] MyProcesses = Process.GetProcesses();
// foreach (var MyProcess in MyProcesses)
// {
// try
// {
// if (MyProcess.MainModule.FileName.ToUpper().Trim() == Program.ExeName.ToUpper().Trim())
// {
// MyProcess.Kill();
// MyProcess.WaitForExit();
// }
// }
// catch (Exception)
// { }
// }
// var process = Process.GetProcesses().FirstOrDefault(f => f.MainModule.FileName.ToUpper().Trim() == Program.ExeName.ToUpper().Trim());
// if (process == null) break;
// Thread.Sleep(z == 0 ? 3000 : 500);
// }
// catch (Exception)
// { }
//}
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)
{ }
}
}
}