BuddyJoe
BuddyJoe

Reputation: 71111

Manipulate Windows Services with .NET / C# like Powershell

What library in .NET would allow you to stop, uninstall, unregister, install, and start Windows Services from .NET? I am using .NET 4/4.5.

What does get-service from Powershell call behind the scenes? How can I get access to the same objects/API from .NET?

Upvotes: 1

Views: 650

Answers (2)

Frode F.
Frode F.

Reputation: 54881

You can manipulate existing services with the ServiceController-class. To create an service, you need to use native methods using P/Invoke. See CreateService (advapi32) and DeleteService (advapi32):

That's how the PowerShell cmdlets does it.

You can find examples for using CreateService and DeleteService in C# here.

Upvotes: 0

i3arnon
i3arnon

Reputation: 116558

You can use ServiceController to do most of that (Start, Stop, Pause and so forth). It requires that you add the ServiceProcess.dll. It can't do everything though, so I use command prompt calls like sc create

Examples here: https://stackoverflow.com/a/21187278/885318

Upvotes: 1

Related Questions