Exemplo n.º 1
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Program_UnhandledException);
            Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Program_UnhandledException);

            InstallerServer server       = new InstallerServer();
            string          optionalArgs = string.Empty;

            // 运行服务
            if (args.Length == 0)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new BusinessService(server.WindowsService) };
                ServiceBase.Run(ServicesToRun);
                return;
            }
            else
            {
                optionalArgs = args[0];
            }

            Console.Title           = "PlatformService Installer";
            Console.BackgroundColor = ConsoleColor.Black;

            var color = Console.BackgroundColor;

            Console.BackgroundColor = ConsoleColor.DarkBlue;

            try
            {
                if (!string.IsNullOrEmpty(optionalArgs))
                {
                    switch (optionalArgs.ToLower())
                    {
                    case "/?":
                    case "/h":
                    case "/help":
                    case "-?":
                    case "-h":
                    case "-help":
                        PrintHelp();
                        break;

                    case "/l":
                    case "/list":
                    case "-l":
                    case "-list":
                    {
                        string contains = null;
                        string status   = null;
                        if (args.Length >= 2)
                        {
                            contains = args[1].Trim();
                        }
                        if (args.Length >= 3)
                        {
                            status = args[2].Trim();
                        }
                        server.ListService(contains, status);
                    }
                    break;

                    case "/c":
                    case "/console":
                    case "-c":
                    case "-console":
                    {
                        if (server.StartConsole())
                        {
                            Console.ReadLine();
                            server.StopConsole();
                        }
                        else
                        {
                            Console.ReadLine();
                        }
                    }
                    break;

                    case "/s":
                    case "/start":
                    case "-s":
                    case "-start":
                    {
                        string service = null;
                        if (args.Length == 2)
                        {
                            service = args[1].Trim();
                        }
                        server.StartService(service);
                    }
                    break;

                    case "/p":
                    case "/stop":
                    case "-p":
                    case "-stop":
                    {
                        string service = null;
                        if (args.Length == 2)
                        {
                            service = args[1].Trim();
                        }
                        server.StopService(service);
                    }
                    break;

                    case "/r":
                    case "/restart":
                    case "-r":
                    case "-restart":
                    {
                        string service = null;
                        if (args.Length == 2)
                        {
                            service = args[1].Trim();
                        }
                        server.StopService(service);
                        server.StartService(service);
                    }
                    break;

                    case "/i":
                    case "/install":
                    case "-i":
                    case "-install":
                        server.InstallService();
                        break;

                    case "/u":
                    case "/uninstall":
                    case "-u":
                    case "-uninstall":
                        server.UninstallService();
                        break;

                    default:
                        Console.WriteLine("输入的命令无效,输入/?显示帮助!");
                        break;
                    }
                }
            }
            finally
            {
                Console.BackgroundColor = color;
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Program_UnhandledException);
            Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Program_UnhandledException);

            InstallerServer server = new InstallerServer();
            string optionalArgs = string.Empty;

            // 运行服务
            if (args.Length == 0)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new BusinessService(server.WindowsService) };
                ServiceBase.Run(ServicesToRun);
                return;
            }
            else
            {
                optionalArgs = args[0];
            }

            Console.Title = "PlatformService Installer";
            Console.BackgroundColor = ConsoleColor.Black;

            var color = Console.BackgroundColor;
            Console.BackgroundColor = ConsoleColor.DarkBlue;

            try
            {
                if (!string.IsNullOrEmpty(optionalArgs))
                {
                    switch (optionalArgs.ToLower())
                    {
                        case "/?":
                        case "/h":
                        case "/help":
                        case "-?":
                        case "-h":
                        case "-help":
                            PrintHelp();
                            break;
                        case "/l":
                        case "/list":
                        case "-l":
                        case "-list":
                            {
                                string contains = null;
                                string status = null;
                                if (args.Length >= 2) contains = args[1].Trim();
                                if (args.Length >= 3) status = args[2].Trim();
                                server.ListService(contains, status);
                            }
                            break;
                        case "/c":
                        case "/console":
                        case "-c":
                        case "-console":
                            {
                                if (server.StartConsole())
                                {
                                    Console.ReadLine();
                                    server.StopConsole();
                                }
                                else
                                {
                                    Console.ReadLine();
                                }
                            }
                            break;
                        case "/s":
                        case "/start":
                        case "-s":
                        case "-start":
                            {
                                string service = null;
                                if (args.Length == 2) service = args[1].Trim();
                                server.StartService(service);
                            }
                            break;
                        case "/p":
                        case "/stop":
                        case "-p":
                        case "-stop":
                            {
                                string service = null;
                                if (args.Length == 2) service = args[1].Trim();
                                server.StopService(service);
                            }
                            break;
                        case "/r":
                        case "/restart":
                        case "-r":
                        case "-restart":
                            {
                                string service = null;
                                if (args.Length == 2) service = args[1].Trim();
                                server.StopService(service);
                                server.StartService(service);
                            }
                            break;
                        case "/i":
                        case "/install":
                        case "-i":
                        case "-install":
                            server.InstallService();
                            break;
                        case "/u":
                        case "/uninstall":
                        case "-u":
                        case "-uninstall":
                            server.UninstallService();
                            break;
                        default:
                            Console.WriteLine("输入的命令无效,输入/?显示帮助!");
                            break;
                    }
                }
            }
            finally
            {
                Console.BackgroundColor = color;
            }
        }