public void PreTest()
{
SetServiceLaunch(); // 修改服务的启动类型
ServiceController sc = new ServiceController("GFIProxy");
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
sc.Start(); //启动服务
}
}
public void PostTest()
{
SetServiceLaunch();
ServiceController sc = new ServiceController("GFIProxy");
if ((sc.Status.Equals(ServiceControllerStatus.Running)) || (sc.Status.Equals(ServiceControllerStatus.StartPending)))
{
sc.Stop();
sc.Refresh();
}
}
public void SetServiceLaunch()
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\GFIProxy", true);
key.SetValue("Start", 3, RegistryValueKind.DWord); // 3 is 手动启动
}