1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| private static bool IsServiceEnabled(string serviceName)
{
try
{
//this is 10X less fast
//Process [] localByName = Process.GetProcessesByName(serviceName);
//return localByName.Length > 0;
Process p = new Process();
p.StartInfo.FileName = "NET.EXE";
p.StartInfo.Arguments = "START";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
return (output.IndexOf("serviceName") > 0);
}
catch(Exception e)
{
MessageBox.Show(e.Message,"IsServiceEnabled Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
throw;
}
}
public static void StartService(string serviceName)
{
try
{
Process p = new Process();
p.StartInfo.FileName = "NET.EXE";
p.StartInfo.Arguments = "START "+serviceName;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
}
catch(Exception e)
{
MessageBox.Show(e.Message,"StartService Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
throw;
}
}
public static void StopService(string serviceName)
{
try
{
Process p = new Process();
p.StartInfo.FileName = "NET.EXE";
p.StartInfo.Arguments = "STOP "+serviceName;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
}
catch(Exception e)
{
MessageBox.Show(e.Message,"StopService Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
throw;
}
} |
Partager