private async void fTapChecker_Load(object sender, EventArgs e) { bool overall = true; try { lbMsg.Text = @"检查Tap-Windows是否安装……"; if (TapDeviceFunc.CheckTapWindowsIntall()) { lbMsg.Text += "OK\n"; } else { lbMsg.Text += "Fail\n"; overall = false; lbMsg.Text += @"正在安装Tap设备……"; Task t = new Task(TapDeviceFunc.InstallTapDriver); t.Start(); await t; lbMsg.Text += "OK\n"; overall = true; } lbMsg.Text += @"获取Tap设备……"; var adapter = TapDeviceFunc.GetTapDevice(); if (adapter != null) { lbMsg.Text += "OK\n"; lbMsg.Text += @"检查Tap设备名……"; if (Regex.IsMatch(adapter.Name, @"^[a-zA-Z0-9]+$")) { lbMsg.Text += "OK\n"; } else { lbMsg.Text += $"Fail({adapter.Name})\n"; overall = false; lbMsg.Text += @"更改Tap设备名……"; Task t = new Task(() => { TapDeviceFunc.RenameTapDevice(TapDeviceFunc.DefaultTapDeviceName, adapter); }); t.Start(); await t; lbMsg.Text += "OK\n"; overall = true; } } else { lbMsg.Text += "Fail\n"; overall = false; lbMsg.Text += @"添加Tap设备……"; Task t = new Task(TapDeviceFunc.AddTapDevice); t.Start(); await t; adapter = TapDeviceFunc.GetTapDevice(); if (adapter == null) { lbMsg.Text += "Fail\n"; lbMsg.Text += @"正在安装Tap设备……"; t = new Task(TapDeviceFunc.InstallTapDriver); t.Start(); await t; lbMsg.Text += "OK\n"; } lbMsg.Text += @"获取Tap设备……"; adapter = TapDeviceFunc.GetTapDevice(); if (adapter == null) { lbMsg.Text += "Fail\n"; overall = false; Logger.Error("Can not get TapDevice anyway"); TapDeviceFunc.TraceNetworkInterface(); } else { TapDeviceFunc.RenameTapDevice(TapDeviceFunc.DefaultTapDeviceName, adapter); lbMsg.Text += "OK\n"; overall = true; } } } catch (Exception ex) { Logger.Error(ex.Message); Logger.Debug(ex.StackTrace); } finally { TapDeviceFunc.GetSetupLog(); if (overall) { lbMsg.Text += @"硬件检测成功,一切就绪,点击此处退出程序。"; } else { lbMsg.Text += "\n"; lbMsg.Text += @"硬件检测出错误,修复失败,正在生成诊断报告……"; new DiagnosticProgress().ShowDialog(this); lbMsg.Text += @"生成完毕,点击此处退出程序。"; } lbMsg.Click += (_sender, _e) => { Application.Exit(); }; } }
private static void Main(string[] args) { Logger.Init(args.Length == 1 && args[0] == "-u"); JSON.Parameters.SerializeNullValues = false; JSON.Parameters.UseExtensions = false; Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // handle UI exceptions Application.ThreadException += Application_ThreadException; // handle non-UI exceptions AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.ApplicationExit += Application_ApplicationExit; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Os.Init(); bool hasRun = false; if (args.Length > 0) { if (args[0] == "-d") { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); if (principal.IsInRole(WindowsBuiltInRole.Administrator)) { if (args.Contains("-r")) { Application.ApplicationExit += (sender, e) => { ProcessStartInfo psi = new ProcessStartInfo { FileName = Application.ExecutablePath, Verb = "runas" }; new Process { StartInfo = psi }.Start(); }; } Application.Run(new TapChecker()); hasRun = true; } } else if (args[0] == "-r") { TapDeviceFunc.GetSetupLog(); hasRun = true; } else if (args[0] == "--test") { } else if (args[0] == "--diagnostic") { Application.Run(new DiagnosticProgress()); hasRun = true; } else if (args[0] == "-u") { GetRunningProc()?.WaitForExit(); if (args.Length == 2 && args[1].StartsWith("--from")) { Updater.RemoveDir(args[1].Substring(7)); } else { try { Updater.Updating(); } catch (Exception e) { Logger.Error(e.Message); Logger.Debug(e.StackTrace); MessageBox.Show($@"更新出错:{e.Message},{Environment.NewLine}日志文件保存在{Logger.Logfile}"); } hasRun = true; } } } if (!hasRun) { SelfCheck(); Application.Run(new EntryWindow()); } }