コード例 #1
0
        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());
            }
        }