15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

686 ❘ ChaPTer 25 windOws services<br />

ServiceControllerInfo contains an embedded ServiceController that is set with the constructor of<br />

the ServiceControllerInfo class. There is also a read-only property Controller to access the embedded<br />

ServiceController:<br />

public class ServiceControllerInfo<br />

{<br />

private readonly ServiceController controller;<br />

public ServiceControllerInfo(ServiceController controller)<br />

{<br />

this.controller = controller;<br />

}<br />

public ServiceController Controller<br />

{<br />

get { return controller; }<br />

}<br />

code snippet ServiceControl/ServiceControllerInfo.cs<br />

To display current information about the service, the ServiceControllerInfo class has the read-only<br />

properties DisplayName, ServiceName, ServiceTypeName, <strong>and</strong> ServiceStatusName. The implementation<br />

of the properties DisplayName <strong>and</strong> ServiceName just accesses the properties DisplayName<br />

<strong>and</strong> ServiceName of the underlying ServiceController class. With the implementation of the properties<br />

ServiceTypeName <strong>and</strong> ServiceStatusName, more work is done — the status <strong>and</strong> type of the service<br />

cannot be returned that easily because a string should be displayed instead of a number, which is<br />

what the ServiceController class returns. The property ServiceTypeName returns a string that<br />

represents the type of the service. The ServiceType you get from the property ServiceController<br />

.ServiceType represents a set of flags that can be combined by using the bitwise OR operator. The<br />

InteractiveProcess bit can be set together with Win32OwnProcess <strong>and</strong> Win32ShareProcess. So, first<br />

it is checked if the InteractiveProcess bit is set before continuing to check for the other values. With<br />

services, the string returned will be “Win32 Service Process” or “Win32 Shared Process”:<br />

public string ServiceTypeName<br />

{<br />

get<br />

{<br />

ServiceType type = controller.ServiceType;<br />

string serviceTypeName = "";<br />

if ((type & ServiceType.InteractiveProcess) != 0)<br />

{<br />

serviceTypeName = "Interactive ";<br />

type -= ServiceType.InteractiveProcess;<br />

}<br />

switch (type)<br />

{<br />

case ServiceType.Adapter:<br />

serviceTypeName += "Adapter";<br />

break;<br />

case ServiceType.FileSystemDriver:<br />

case ServiceType.KernelDriver:<br />

case ServiceType.RecognizerDriver:<br />

serviceTypeName += "Driver";<br />

break;<br />

case ServiceType.Win32OwnProcess:<br />

serviceTypeName += "Win32 Service Process";<br />

break;<br />

case ServiceType.Win32ShareProcess:<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!