Reputation: 131
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
Upvotes: 3
Views: 5455
Reputation: 343
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);
Upvotes: 1
Reputation: 3478
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);
Upvotes: 0