How to programmatically configure an installed Win

2019-07-30 20:07发布

问题:

I have a .Net C# Windows service app that works nicely now, and installs itself properly. I also have a small WinForms tool to provide a friendly interface to modify its config file.

It occurs to me that a nice touch would be to extend this tool to configure the service itself as well (the stuff you do in the Services admin tool, eg Startup mode, Windows login account credentials, etc).

Can this be done relatively easily with the .Net framework?

回答1:

You can accomplish a lot using the Service Controller class. It enables Starting/Stopping etc.

The rest you can do by calling the sc tool the same way you would using a console:

var process = Process.Start(
    "sc", 
    "failure \"ServiceName\" reset= 60 actions= restart/1000/restart/1000");
process.WaitForExit();


回答2:

You cannot use .net directly to do this as far as I know. But you can call the Win32 Api for methods to accomplish it. An example is posted on this Blog. You can probably also find more functionality over at pinvoke



标签: c# .net service