Beispiel #1
0
        public static void Run(string[] args)
        {
            if (args.Length > 0) {
            var d = new ServiceDescriptor();
            Win32Services svc = new WmiRoot().GetCollection<Win32Services>();
            Win32Service s = svc.Select(d.Id);

            args[0] = args[0].ToLower();
            if (args[0] == "install") {
              svc.Create(
              d.Id,
              d.Caption,
              "\"" + ServiceDescriptor.ExecutablePath + "\"",
              WMI.ServiceType.OwnProcess,
              ErrorControl.UserNotified,
              StartMode.Automatic,
              d.Interactive,
              d.ServiceDependencies);
              // update the description
              /* Somehow this doesn't work, even though it doesn't report an error
              Win32Service s = svc.Select(d.Id);
              s.Description = d.Description;
              s.Commit();
               */

              // so using a classic method to set the description. Ugly.
              Registry.LocalMachine.OpenSubKey("System").OpenSubKey("CurrentControlSet").OpenSubKey("Services")
              .OpenSubKey(d.Id, true).SetValue("Description", d.Description);
            }
            if (args[0] == "uninstall") {
              if (s == null)
            return; // there's no such service, so consider it already uninstalled
              try {
            s.Delete();
              }
              catch (WmiException e) {
            if (e.ErrorCode == ReturnValue.ServiceMarkedForDeletion)
              return; // it's already uninstalled, so consider it a success
            throw e;
              }
            }
            if (args[0] == "start") {
              if (s == null) ThrowNoSuchService();
              s.StartService();
            }
            if (args[0] == "stop") {
              if (s == null) ThrowNoSuchService();
              s.StopService();
            }
            if (args[0] == "restart") {
              if (s == null)
            ThrowNoSuchService();

              if (s.Started)
            s.StopService();

              while (s.Started) {
            Thread.Sleep(1000);
            s = svc.Select(d.Id);
              }

              s.StartService();
            }
            if (args[0] == "status") {
              if (s == null)
            Console.WriteLine("NonExistent");
              else if (s.Started)
            Console.WriteLine("Started");
              else
            Console.WriteLine("Stopped");
            }
            if (args[0] == "test") {
              WrapperService wsvc = new WrapperService();
              wsvc.OnStart(args);
              Thread.Sleep(1000);
              wsvc.OnStop();
            }
            return;
              }
              ServiceBase.Run(new WrapperService());
        }
Beispiel #2
0
        public static void Run(string[] args)
        {
            if (args.Length > 0)
            {
                var           d   = new ServiceDescriptor();
                Win32Services svc = new WmiRoot().GetCollection <Win32Services>();
                Win32Service  s   = svc.Select(d.Id);

                args[0] = args[0].ToLower();
                if (args[0] == "install")
                {
                    svc.Create(
                        d.Id,
                        d.Caption,
                        "\"" + ServiceDescriptor.ExecutablePath + "\"",
                        WMI.ServiceType.OwnProcess,
                        ErrorControl.UserNotified,
                        StartMode.Automatic,
                        d.Interactive,
                        d.ServiceDependencies);
                    // update the description

                    /* Somehow this doesn't work, even though it doesn't report an error
                     * Win32Service s = svc.Select(d.Id);
                     * s.Description = d.Description;
                     * s.Commit();
                     */

                    // so using a classic method to set the description. Ugly.
                    Registry.LocalMachine.OpenSubKey("System").OpenSubKey("CurrentControlSet").OpenSubKey("Services")
                    .OpenSubKey(d.Id, true).SetValue("Description", d.Description);
                }
                if (args[0] == "uninstall")
                {
                    if (s == null)
                    {
                        return; // there's no such service, so consider it already uninstalled
                    }
                    try {
                        s.Delete();
                    }
                    catch (WmiException e) {
                        if (e.ErrorCode == ReturnValue.ServiceMarkedForDeletion)
                        {
                            return; // it's already uninstalled, so consider it a success
                        }
                        throw e;
                    }
                }
                if (args[0] == "start")
                {
                    if (s == null)
                    {
                        ThrowNoSuchService();
                    }
                    s.StartService();
                }
                if (args[0] == "stop")
                {
                    if (s == null)
                    {
                        ThrowNoSuchService();
                    }
                    s.StopService();
                }
                if (args[0] == "restart")
                {
                    if (s == null)
                    {
                        ThrowNoSuchService();
                    }

                    if (s.Started)
                    {
                        s.StopService();
                    }

                    while (s.Started)
                    {
                        Thread.Sleep(1000);
                        s = svc.Select(d.Id);
                    }

                    s.StartService();
                }
                if (args[0] == "status")
                {
                    if (s == null)
                    {
                        Console.WriteLine("NonExistent");
                    }
                    else if (s.Started)
                    {
                        Console.WriteLine("Started");
                    }
                    else
                    {
                        Console.WriteLine("Stopped");
                    }
                }
                if (args[0] == "test")
                {
                    WrapperService wsvc = new WrapperService();
                    wsvc.OnStart(args);
                    Thread.Sleep(1000);
                    wsvc.OnStop();
                }
                return;
            }
            ServiceBase.Run(new WrapperService());
        }