using DevExpress.LookAndFeel; using DevExpress.XtraEditors; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Security.Principal; using System.Windows.Forms; using XdCxRhDW.Framework; using XdCxRhDW.UI.Lib; namespace XdCxRhDW { internal static class Program { static Program() { //设置私有路径 Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "AddIns;"); var m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static); var funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance); m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", "AddIns;" }); //c++dll加入环境变量 string paths = Environment.GetEnvironmentVariable("PATH"); var dirs = Directory.EnumerateDirectories("AddIns", "*", SearchOption.AllDirectories); List list = new List { Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns") }; foreach (var item in dirs) { list.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item)); } Environment.SetEnvironmentVariable("PATH", $"{paths};{string.Join(";", list)}"); AppDomain.CurrentDomain.UnhandledException += (sender, e) => { var ex = e.ExceptionObject as Exception; while (ex.InnerException != null) ex = ex.InnerException; XdCxRhDW.Framework.LogHelper.Error("出现未处理的异常,程序即将退出!", ex); DxHelper.MsgBoxHelper.ShowError("出现未处理的异常,程序即将退出!"); }; Application.ThreadException += (sender, e) => { var ex = e.Exception; while (ex.InnerException != null) ex = ex.InnerException; DxHelper.MsgBoxHelper.ShowError($"出现未处理的线程异常!{e.Exception.Message}"); LogUI.Error("出现未处理的线程异常", e.Exception).Wait(5000); }; } static bool IsRunningAsAdmin() { bool result; try { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); result = principal.IsInRole(WindowsBuiltInRole.Administrator); } catch { result = false; } return result; } static void RestartAsAdmin() { var startInfo = new ProcessStartInfo(); startInfo.FileName = Application.ExecutablePath; startInfo.Verb = "runas"; // 以管理员身份运行 try { Process.Start(startInfo); } catch (System.ComponentModel.Win32Exception) { // 用户取消了管理员权限提示,或者其他错误 // 可以在此处处理错误情况 } Application.Exit(); } //win10及以上版本管理员运行无法访问网络映射盘等,需要修改注册表并且重启设备 static void CheckUACReg() { try { RegistryKey key = Registry.LocalMachine; RegistryKey system = key.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", true); if (system == null) { system = key.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System"); } object obj = system.GetValue("EnableLinkedConnections"); if (obj == null || (int)obj != 1) { system.SetValue("EnableLinkedConnections", Convert.ToInt32(1), RegistryValueKind.DWord); } } catch (Exception ex) { XdCxRhDW.Framework.LogHelper.Error("修改UAC注册表信息异常", ex); } } /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { WindowsFormsSettings.AllowDpiScale = true; WindowsFormsSettings.AllowHoverAnimation = DevExpress.Utils.DefaultBoolean.True; WindowsFormsSettings.AllowDefaultSvgImages = DevExpress.Utils.DefaultBoolean.True; WindowsFormsSettings.AllowRoundedWindowCorners = DevExpress.Utils.DefaultBoolean.True; WindowsFormsSettings.AnimationMode = AnimationMode.EnableAll; WindowsFormsSettings.BackgroundSkinningMode = BackgroundSkinningMode.AllColors; WindowsFormsSettings.DefaultAllowHtmlDraw = true; WindowsFormsSettings.DefaultLookAndFeel.SetSkinStyle(SkinStyle.WXICompact); WindowsFormsSettings.DefaultFont = new System.Drawing.Font("微软雅黑", 10f); WindowsFormsSettings.SetPerMonitorDpiAware(); if (Debugger.IsAttached) { //DevExpress23.2以上版本查看未本地化的资源 DevExpress.Utils.Localization.XtraLocalizer.EnableTraceSource(); } if (IsRunningAsAdmin()) { CheckUACReg(); string screenTitle = AppConfigHelper.Get("SystemName", ""); string screenCompany = AppConfigHelper.Get("Company", ""); DxHelper.WaitHelper.SetSplashTips("Tips.txt"); ChsLocalizer.UseChs(); DxHelper.WaitHelper.ShowSplashScreen(screenTitle, screenCompany); DxHelper.WaitHelper.UpdateSplashMessage("正在加载程序资源文件..."); MainForm mainForm = new MainForm() { Text = screenTitle }; DxHelper.WaitHelper.UpdateSplashMessage("正在初始化..."); System.Windows.Forms.Application.Run(mainForm); } else { RestartAsAdmin(); } } } }