123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using DevExpress.LookAndFeel;
- using DevExpress.XtraEditors;
- using Serilog;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Security.Principal;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using XdCxRhDW.App;
- using XdCxRhDW.Dto;
- 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<string> list = new List<string>
- {
- 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 args = e.ExceptionObject as Exception;
- Serilog.Log.Error(args, "出现未处理的异常,程序即将退出!");
- DxHelper.MsgBoxHelper.ShowError("出现未处理的异常,程序即将退出!");
- };
- Application.ThreadException += (sender, e) =>
- {
- DxHelper.MsgBoxHelper.ShowError($"出现未处理的线程异常!{e.Exception.Message}");
- Serilog.Log.Error(e.Exception, "出现未处理的线程异常");
- };
- }
- 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(string[] args)
- {
- var startInfo = new ProcessStartInfo();
- startInfo.FileName = Application.ExecutablePath;
- startInfo.Arguments =string.Join(" ",args);
- startInfo.Verb = "runas"; // 以管理员身份运行
- try
- {
- Process.Start(startInfo);
- }
- catch (System.ComponentModel.Win32Exception)
- {
- // 用户取消了管理员权限提示,或者其他错误
- // 可以在此处处理错误情况
- }
- Application.Exit();
- }
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main(string[] args)
- {
- if (args.Length > 0 && args[0].Trim().ToLower()=="debug")
- {
- AppConst.Debug = true;
- }
- string outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine} {Exception}";
- Serilog.Log.Logger = new Serilog.LoggerConfiguration()
- .WriteTo.Console(outputTemplate: outputTemplate)
- .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
- .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day))
- .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
- .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day))
- .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
- .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
- .CreateLogger();
- 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())
- {
- string screenTitle = ConfigurationManager.AppSettings["SystemName"];
- string screenCompany = ConfigurationManager.AppSettings["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(args);
- }
- }
- }
- }
|