ExecuteCommand 服务控制器 - 无法在服务上执行命令

发布于 2024-11-03 08:22:52 字数 853 浏览 3 评论 0原文

我正在使用服务控制器的executecommand 函数,如下所示:

            ServiceController serviceController = new ServiceController("a Service",
                Environment.MachineName);

            serviceController.ExecuteCommand(129);

在服务控制器中:

    protected override void OnCustomCommand(int command)
    {
        base.OnCustomCommand(command);

        // Depending on the integer passed in, the appropriate method is called.
        switch (command)
        {
            case 129:
                RestartSpooler();
                break;
            case 131:
                InstallPrinter();
                break;
            case 132:
                DeletePrinter();
                break;
        }
    }

但是,尽管从调用代码中调用了任何命令(代码命中线路,然后跳过,没有例外),但没有任何反应。为什么?这一切都在本地计算机上,我拥有完整的管理员权限。

谢谢

I am using the service controller executecommand function, like so:

            ServiceController serviceController = new ServiceController("a Service",
                Environment.MachineName);

            serviceController.ExecuteCommand(129);

And in the service controller:

    protected override void OnCustomCommand(int command)
    {
        base.OnCustomCommand(command);

        // Depending on the integer passed in, the appropriate method is called.
        switch (command)
        {
            case 129:
                RestartSpooler();
                break;
            case 131:
                InstallPrinter();
                break;
            case 132:
                DeletePrinter();
                break;
        }
    }

However, despite calling any of the commands from the calling code (the code hits the line, then steps over, no exceptions), nothing happens. Why? This is all on the local machine and I have full admin rights.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-11-10 08:22:52

您必须尝试对已停止的服务执行命令。添加如下内容:

    if (serviceController1.Status == ServiceControllerStatus.Stopped)
    {
        serviceController1.Start();
    }
    serviceController1.ExecuteCommand(192);

You must be trying to execute command against a stopped service. Add something like the following:

    if (serviceController1.Status == ServiceControllerStatus.Stopped)
    {
        serviceController1.Start();
    }
    serviceController1.ExecuteCommand(192);
勿忘初心 2024-11-10 08:22:52

我还没有找到任何它不起作用的原因。
这是使用自定义命令的 Windows 服务的工作示例

public partial class TestService : ServiceBase
{
    public TestService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args) { }

    protected override void OnStop() { }

    protected override void OnCustomCommand(int command)
    {
        base.OnCustomCommand(command);

        switch (command)
        {
            case 129:
                //
                break;
            case 131:
                //
                break;
            case 132:
                //
                break;
        }
    }
}

服务安装程序

[RunInstaller(true)]
public partial class Installer : System.Configuration.Install.Installer
{
    public Installer()
    {
        InitializeComponent();

        _processInstaller = new ServiceProcessInstaller();
        _processInstaller.Account = ServiceAccount.LocalSystem;

        _serviceInstaller = new ServiceInstaller();
        _serviceInstaller.StartType = ServiceStartMode.Manual;
        _serviceInstaller.ServiceName = "TestService";

        Installers.Add(_serviceInstaller);
        Installers.Add(_processInstaller);
    }

    private readonly ServiceInstaller _serviceInstaller;
    private readonly ServiceProcessInstaller _processInstaller;
}

服务使用情况

var serviceController = new ServiceController("TestService", Environment.MachineName);
serviceController.ExecuteCommand(129);

I haven't found any reason why it shouldn't work.
Here is the working example of windows service with custom command

public partial class TestService : ServiceBase
{
    public TestService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args) { }

    protected override void OnStop() { }

    protected override void OnCustomCommand(int command)
    {
        base.OnCustomCommand(command);

        switch (command)
        {
            case 129:
                //
                break;
            case 131:
                //
                break;
            case 132:
                //
                break;
        }
    }
}

Service installer

[RunInstaller(true)]
public partial class Installer : System.Configuration.Install.Installer
{
    public Installer()
    {
        InitializeComponent();

        _processInstaller = new ServiceProcessInstaller();
        _processInstaller.Account = ServiceAccount.LocalSystem;

        _serviceInstaller = new ServiceInstaller();
        _serviceInstaller.StartType = ServiceStartMode.Manual;
        _serviceInstaller.ServiceName = "TestService";

        Installers.Add(_serviceInstaller);
        Installers.Add(_processInstaller);
    }

    private readonly ServiceInstaller _serviceInstaller;
    private readonly ServiceProcessInstaller _processInstaller;
}

Service usage

var serviceController = new ServiceController("TestService", Environment.MachineName);
serviceController.ExecuteCommand(129);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文