using ConUpdate.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ConUpdate
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private CommonMethod common = new CommonMethod();
private ProgressViewModel pvm = new ProgressViewModel();
private LogHelper log = new LogHelper();
App app = ((App)Application.Current);
bool killexe = false;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
log.WriteInfoFile("Loading...");
this.DataContext = pvm;
try
{
log.DeleteFiles();
List updateList = new List();
Action ac = new Action(() =>
{
string currentVersion = string.Empty;
updateList = common.IsUpdate(out currentVersion);
});
ac.BeginInvoke((o) =>
{
if (updateList.Count > 0)
{
killExE();
Thread.Sleep(2000);
common.StartUpdate(updateList[0], pvm);
}
this.Dispatcher.Invoke(new Action(() =>
{
this.Close();
}));
}, updateList);
}
catch (Exception ex)
{
log.WriteInfoFile(ex.Message + ex.StackTrace);
}
}
private void killExE()
{
try
{
Process[] pro = Process.GetProcesses();
for (int i = 0; i < pro.Length; i++)
{
if (pro[i].ProcessName.ToLower() == "pcscreensavers.exe")
{
pro[i].Kill();//结束进程
killexe = true;
log.WriteInfoFile("进程关闭");
return;
}
}
}
catch (Exception ex)
{
log.WriteInfoFile(ex.ToString());
}
}
private void Window_Closed(object sender, EventArgs e)
{
//if (killexe)
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
System.Windows.Forms.Application.EnableVisualStyles();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (!principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").Substring(0, System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").LastIndexOf("/"));//设置应用程序在其所在目录
startInfo.FileName = startInfo.WorkingDirectory + "/Container.exe"; //设置启动参数
startInfo.Arguments = app.info + "&true";
log.WriteInfoFile("发送" + startInfo.Arguments);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
//log.WriteLogFile("工作路径:" + startInfo.WorkingDirectory, "APPError");
System.Diagnostics.Process.Start(startInfo);
//退出
System.Windows.Forms.Application.Exit();
//System.Diagnostics.Process p = new System.Diagnostics.Process();
//p.StartInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").Substring(0, System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").LastIndexOf("/"));//设置应用程序在其所在目录
//p.StartInfo.FileName = p.StartInfo.WorkingDirectory + "/PCScreenSavers.exe";
//p.StartInfo.Arguments = app.info;
//p.StartInfo.Verb = "runas";
//p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
//p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
//p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
//p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
////p.ProcessNa = "PCScreenUpdate";
//p.StartInfo.CreateNoWindow = false;//不显示程序窗口
//p.Start();//启动程序
}
else
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").Substring(0, System.Windows.Forms.Application.ExecutablePath.Replace("\\", "/").LastIndexOf("/"));//设置应用程序在其所在目录
p.StartInfo.FileName = p.StartInfo.WorkingDirectory + "/Container.exe";
p.StartInfo.Arguments = app.info + "&true";
log.WriteInfoFile("发送" + p.StartInfo.Arguments);
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = false;//不显示程序窗口
p.Start();//启动程序
}
}
}
}
}