c#如何控制系统服务的重启!

解决方案 »

  1.   

    net start 服务名字
    或者
    类似ServiceController sc  = new ServiceController();
    sc.ServiceName = "W3SVC";
    if (sc.Status == ServiceControllerStatus.Stopped)
    {
       // Start the service if the current status is stopped.   Console.WriteLine("Starting the Alerter service...");
       try 
       {
          // Start the service, and wait until its status is "Running".
          sc.Start();
          sc.WaitForStatus(ServiceControllerStatus.Running);
       
          // Display the current service status.
          Console.WriteLine("The www 服务 status is now set to {0}.", 
                             sc.Status.ToString());
       }
       catch (InvalidOperationException)
       {
          Console.WriteLine("Could not start the Alerter service.");
       }
    }
      

  2.   

    可是在我的开发环境中显示ServiceController没有这个啊
      

  3.   

    我这里没有找到System.ServiceProcess这个名称空间!!
      

  4.   

    添加对System.ServiceProcess.dll的引用
      

  5.   

    1.通过net start ,net stop 命令行程序在cmd中调用,文章
    http://blog.csdn.net/zhzuo/archive/2004/03/21/22024.aspx
    2.使用System.ServiceProcess.ServiceController 类来操作。
    3.使用System.Wmi来访问控制,例子,
    http://blog.csdn.net/zhzuo/archive/2004/03/29/22025.aspx