下面的示例使用 ServiceController 类检查 Telnet 服务的当前状态。如果该服务已停止,此示例将启动该服务。如果该服务正在运行,此示例将停止该服务。 // Toggle the Telnet service - // If it is started (running, paused, etc), stop the service. // If it is stopped, start the service. ServiceController sc = new ServiceController( "Telnet "); Console.WriteLine( "The Telnet service status is currently set to {0} ", sc.Status.ToString()); if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending))) { // Start the service if the current status is stopped. Console.WriteLine( "Starting the Telnet service... "); sc.Start(); } else { // Stop the service if its status is not set to "Stopped ". Console.WriteLine( "Stopping the Telnet service... "); sc.Stop(); } // Refresh and display the current service status. sc.Refresh(); Console.WriteLine( "The Telnet service status is now set to {0}. ", sc.Status.ToString());
其实你可以google一下,很简单
public static void Excute(string cmdText) { Process p = new Process();
// If it is started (running, paused, etc), stop the service.
// If it is stopped, start the service.
ServiceController sc = new ServiceController( "Telnet ");
Console.WriteLine( "The Telnet service status is currently set to {0} ",
sc.Status.ToString()); if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
// Start the service if the current status is stopped. Console.WriteLine( "Starting the Telnet service... ");
sc.Start();
}
else
{
// Stop the service if its status is not set to "Stopped ". Console.WriteLine( "Stopping the Telnet service... ");
sc.Stop();
} // Refresh and display the current service status.
sc.Refresh();
Console.WriteLine( "The Telnet service status is now set to {0}. ",
sc.Status.ToString());
public static void Excute(string cmdText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine( "\ " "+cmdText+ "\ " ");
p.StandardInput.WriteLine( "exit ");
while(!p.HasExited)
{
p.WaitForExit();
}
p.Close();
}
p.StartInfo.CreateNoWindow = false;
p.StartInfo.RedirectStandardInput = false;
p.StartInfo.RedirectStandardOutput = false;
p.startinfo.windowstyle=normal(记忆着写的)