安装、启动一个Windows服务
Console.WriteLine("创建 " + WindowsService.ServiceName);
var info = new ProcessStartInfo("sc", string.Format("create {0} binpath= \"{1}\" displayname= \"{2}\" start= auto type= own",
WindowsService.ServiceName, Process.GetCurrentProcess().MainModule.FileName, WindowsService.ServiceName))
{
WindowStyle = ProcessWindowStyle.Minimized,
UseShellExecute = false
};
var pro = Process.Start(info);
pro.WaitForExit();
Console.WriteLine("启动 " + WindowsService.ServiceName);
info.Arguments = "start " + WindowsService.ServiceName;
pro = Process.Start(info);
pro.WaitForExit();
停止、卸载一个Windows服务
Console.WriteLine("停止 " + WindowsService.ServiceName);
var info = new ProcessStartInfo("sc", "stop " + WindowsService.ServiceName)
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = 独立窗口
};
var pro = Process.Start(info);
pro.WaitForExit();
Console.WriteLine("卸载 " + WindowsService.ServiceName);
info.Arguments = "delete " + WindowsService.ServiceName;
pro = Process.Start(info);
pro.WaitForExit();