- using System.Diagnostics;
- /// <summary>
- /// Starts a Windows service on the local machine.
- /// </summary>
- /// <param name=\"name\">Name of the Windows service; must be the display name of the service (not the service .exe name)</param>
- /// <returns>True if the service was successfully started, false otherwise.</returns>
- public static bool StartService(string name)
- {
- string executable = \"net.exe\";
- string args = \"start \\\"\" + name + \"\\\"\";
- try
- {
- Process p = Process.Start(executable, args);
- p.EnableRaisingEvents = true;
- p.WaitForExit();
- if (p.ExitCode == 0)
- {
- return true;
- }
- return false;
- }
- // If the service can\'t be found, the runtime throws a Win32Exception
- catch (Win32Exception ex)
- {
- throw;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/12112012274.html
来源: http://www.codesnippet.cn/detail/12112012274.html