211 lines
7.9 KiB
C#
211 lines
7.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows;
|
|
using Lib = ESSupport.Lib;
|
|
|
|
namespace RunUpdateExe
|
|
{
|
|
/// <summary>
|
|
/// App.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
[DllImport("user32", CharSet = CharSet.Unicode)]
|
|
static extern IntPtr FindWindow(string cls, string win);
|
|
[DllImport("user32")]
|
|
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
|
|
[DllImport("user32")]
|
|
static extern bool IsIconic(IntPtr hWnd);
|
|
[DllImport("user32")]
|
|
static extern bool OpenIcon(IntPtr hWnd);
|
|
System.Threading.Mutex mutex;
|
|
|
|
/// <summary>
|
|
/// 程序入口
|
|
/// </summary>
|
|
public App()
|
|
{
|
|
DispatcherUnhandledException += App_DispatcherUnhandledException;
|
|
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
|
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
|
|
Startup += new StartupEventHandler(App_Startup);
|
|
}
|
|
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
//MessageBox.Show(e.Exception.ToString());
|
|
//Shutdown(1);
|
|
//try
|
|
//{
|
|
// Lib.LogHelper.WriteServiceLog("主线程错误:" + e.Exception.ToString());
|
|
//}
|
|
//catch { }
|
|
e.Handled = true;
|
|
}
|
|
|
|
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
//Loger.WriteErrorLog("捕获未处理异常:" + e.ExceptionObject);
|
|
//try
|
|
//{
|
|
// Lib.LogHelper.WriteServiceLog("子线程错误:" + (e.ExceptionObject as Exception).ToString());
|
|
//}
|
|
//catch { }
|
|
//MessageBox.Show(e.ExceptionObject.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 程序启动初始化
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
void App_Startup(object sender, StartupEventArgs e)
|
|
{
|
|
mutex = new System.Threading.Mutex(true, "RunUpdateExeNew", out bool ret);
|
|
if (!ret)
|
|
{
|
|
ActivateOtherWindow();
|
|
Environment.Exit(0);
|
|
}
|
|
else
|
|
{
|
|
string _ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "Update.config";
|
|
string _DLLFilePath = "";
|
|
string _DLLDir = "";
|
|
string _LocalSitePort = Lib.ConfigHelper.GetAppConfig(_ConfigPath, "LocalSitePort");
|
|
if (String.IsNullOrWhiteSpace(_LocalSitePort))
|
|
{
|
|
_LocalSitePort = "11000";
|
|
}
|
|
Hashtable _Hashtable = Lib.IISWorker.GetWebPathAndWebName(_LocalSitePort);
|
|
if (_Hashtable.ContainsKey("name"))
|
|
{
|
|
_DLLFilePath = _Hashtable["path"].ToString() + "\\bin\\EShang.dll";
|
|
_DLLDir = _Hashtable["path"].ToString() + "\\bin\\";
|
|
}
|
|
else
|
|
{
|
|
_DLLFilePath = AppDomain.CurrentDomain.BaseDirectory + "bin\\EShang.dll";
|
|
_DLLDir = AppDomain.CurrentDomain.BaseDirectory + "bin\\";
|
|
}
|
|
if (File.Exists(_DLLFilePath))
|
|
{
|
|
try
|
|
{
|
|
byte[] _File = File.ReadAllBytes(_DLLFilePath);
|
|
Assembly _Assembly = Assembly.Load(_File);////我们要调用的dll文件路径
|
|
Type _Type = _Assembly.GetType("EShang.Update");//获取类名,必须 命名空间+类名
|
|
//(Activator.CreateInstance(_Type) as Window).Show();
|
|
//实例化类型
|
|
object _object = Activator.CreateInstance(_Type);
|
|
|
|
//得到要调用的某类型的方法
|
|
MethodInfo method = _Type.GetMethod("LoadAssembly");//functionname:方法名字
|
|
object[] _Parameters = { "RunUpdateExe", _DLLDir };
|
|
//对方法进行调用
|
|
var keyData = method.Invoke(_object, _Parameters);//param为方法参数object数组
|
|
if ((bool)keyData == false)
|
|
{
|
|
MainWindow _MainWindow = new MainWindow();
|
|
_MainWindow.Show();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
MainWindow _MainWindow = new MainWindow();
|
|
_MainWindow.Show();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MainWindow _MainWindow = new MainWindow();
|
|
_MainWindow.Show();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换窗口
|
|
/// </summary>
|
|
private static void ActivateOtherWindow()
|
|
{
|
|
var other = FindWindow(null, "系统更新");
|
|
if (other != IntPtr.Zero)
|
|
{
|
|
SetForegroundWindow(other);
|
|
if (IsIconic(other))
|
|
OpenIcon(other);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载内嵌资源DLL文件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="args"></param>
|
|
/// <returns></returns>
|
|
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
|
{
|
|
Assembly executingAssembly = Assembly.GetExecutingAssembly();
|
|
var executingAssemblyName = executingAssembly.GetName();
|
|
var resName = executingAssemblyName.Name + ".resources";
|
|
|
|
AssemblyName assemblyName = new AssemblyName(args.Name);
|
|
string path = "";
|
|
if (resName == assemblyName.Name)
|
|
{
|
|
path = executingAssemblyName.Name + ".g.resources";
|
|
}
|
|
else
|
|
{
|
|
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", assemblyName.Name + ".dll");
|
|
if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
|
|
{
|
|
path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
|
|
}
|
|
}
|
|
|
|
using (Stream stream = executingAssembly.GetManifestResourceStream(path))
|
|
{
|
|
if (stream == null)
|
|
{
|
|
if (File.Exists(path))
|
|
{
|
|
byte[] _File = File.ReadAllBytes(path);
|
|
return Assembly.Load(_File);
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
byte[] assemblyRawBytes = new byte[stream.Length];
|
|
stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
|
|
return Assembly.Load(assemblyRawBytes);
|
|
}
|
|
//string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\");
|
|
//path = Path.Combine(path, args.Name.Split(',')[0]);
|
|
//path = String.Format(@"{0}.dll", path);
|
|
//byte[] _File = File.ReadAllBytes(path);
|
|
//return Assembly.Load(_File);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动事件
|
|
/// </summary>
|
|
/// <param name="e"></param>
|
|
//protected override void OnStartup(StartupEventArgs e)
|
|
//{
|
|
// base.OnStartup(e);
|
|
//}
|
|
}
|
|
}
|
|
|