⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch8_16.cs

📁 《c#技术内幕代码》
💻 CS
字号:
using System;
using System.ServiceProcess;

class CH8_16
{
     public static void StartService( string server, string service )
     {
        try
	{
           Console.WriteLine("About to start the {0} Service", service );
	   ServiceController svcCtrl;
	   
	   if ( server.Length != 0 )
	      svcCtrl = new ServiceController(server, service);
	   else
	      svcCtrl = new ServiceController(service);
	   
           svcCtrl.Start();
	}
        catch(Exception e)
	{
           Console.WriteLine("Cought exception of : {0}", e.ToString());
        }
     }
     
     public static void StopService( string server, string service )
     {
        try
	{
           Console.WriteLine("About to stop the {0} Service", service );
	   ServiceController svcCtrl;
	   
	   if ( server.Length != 0 )
	      svcCtrl = new ServiceController(server, service);
	   else
	      svcCtrl = new ServiceController(service);
	   
           svcCtrl.Stop();
	}
        catch(Exception e)
	{
           Console.WriteLine("Cought exception of : {0}", e.ToString());
        }
     }

     public static void ShowServices( string server )
     {
        try
	{
	   ServiceController[] services;
	   if ( server.Length != 0 )
              services = ServiceController.GetServices(server);
	   else
	      services = ServiceController.GetServices();
	      
           foreach ( ServiceController svc in services)
	   {
               Console.WriteLine("Found service : {0}", svc.DisplayName); 
           }
	}
        catch(Exception e)
	{
           Console.WriteLine("Cought exception of : {0}", e.ToString());
        }
     }
     
     
     public static int Main(string[] args)
     {
         if(args.Length == 0)
         {
             Console.WriteLine("Syntax: service <server> <command> [service_name]");
             return -1;
         }
	 
         if ( args[1] == "Start" )
	 {
	    StartService( args[0], args[2] );
	 }
	 
         if ( args[1] == "Stop" )
	 {
	    StopService( args[0], args[2] );
	 }

	 if ( args[1] == "Show" )
	 {
	    ShowServices( args[0] );
	 }
	 
         return 0;
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -