using System; 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; namespace AutoUpdateEx { /// /// App.xaml 的交互逻辑 /// 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; /// /// 程序入口 /// public App() { DispatcherUnhandledException += App_DispatcherUnhandledException; AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly; Startup += new StartupEventHandler(App_Startup); } private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { try { Lib.LogHelper.WriteServiceLog(e.Exception.ToString()); } catch { } } /// /// 程序启动初始化 /// /// /// void App_Startup(object sender, StartupEventArgs e) { mutex = new System.Threading.Mutex(true, "AutoUpdateEx", out bool ret); if (!ret) { ActivateOtherWindow(); Environment.Exit(0); } else { string _DLLFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EShang.dll"); 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 = { "AutoUpdateEx", AppDomain.CurrentDomain.BaseDirectory }; //对方法进行调用 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(); } } } /// /// 切换窗口 /// private static void ActivateOtherWindow() { var other = FindWindow(null, "系统更新"); if (other != IntPtr.Zero) { SetForegroundWindow(other); if (IsIconic(other)) OpenIcon(other); } } /// /// 加载内嵌资源DLL文件 /// /// /// /// private static Assembly OnResolveAssembly(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 = 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) return null; byte[] assemblyRawBytes = new byte[stream.Length]; stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length); return Assembly.Load(assemblyRawBytes); } } /// /// 启动事件 /// /// protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); } } }