C# realizes the use of tray to control windows service & C# ASP.NET controls the opening and closing and restart of windows service

This example not only realizes the system tray function, but also realizes the control of multiple windows services, which is very practical!

using System;

namespace UBISerialsController
{
 /// <summary>
 /// Summary description of Class1.
 /// </summary>
 public class Class1
 {
  static System.ServiceProcess.ServiceController sc;
  static System.Windows.Forms.NotifyIcon ni;
  static System.Windows.Forms.ContextMenu cm;
  static System.Timers.Timer timer;

  public Class1()
  {
   //
   // TODO: add constructor logic here
   //
   
  }

  static void Main()
  {
   try
   {
    sc=new System.ServiceProcess.ServiceController("UBISerials");
    ni=new System.Windows.Forms.NotifyIcon();
    ni.Visible=false;
    cm=new System.Windows.Forms.ContextMenu();
    cm.MenuItems.Add(new System.Windows.Forms.MenuItem("停止",new EventHandler(StopService)));
    cm.MenuItems.Add(new System.Windows.Forms.MenuItem("暂停",new EventHandler(PauseService)));
    cm.MenuItems.Add(new System.Windows.Forms.MenuItem("继续",new EventHandler(ContiuneService)));
    cm.MenuItems.Add(new System.Windows.Forms.MenuItem("开始",new EventHandler(StartService)));
    cm.MenuItems.Add("-");
    cm.MenuItems.Add(new System.Windows.Forms.MenuItem("关于",new EventHandler(About)));
    cm.MenuItems.Add(new System.Windows.Forms.MenuItem("退出",new EventHandler(Exit)));
    ni.ContextMenu = cm;
    ni.Visible=true;
    SetUpTimer();
    System.Windows.Forms.Application.Run();     
   }
   catch(System.Exception ex)
   {
    System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
   }
  }

  private static void StopService(object sender,System.EventArgs e)
  {
   if(sc.Status==System.ServiceProcess.ServiceControllerStatus.Running&&sc.CanStop==true)
   {
    try
    {
     sc.Stop();
    }
    catch(System.Exception ex)
    {
     System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
    }
   }
  }

  private static void PauseService(object sender,System.EventArgs e)
  {
   if(sc.Status!=System.ServiceProcess.ServiceControllerStatus.Paused&&sc.CanPauseAndContinue==true)
   {
    try

    {
     sc.Pause();
    }
    catch(System.Exception ex)
    {
     System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
    }
   }
  }

  private static void ContiuneService(object sender,System.EventArgs e)
  {
   if(sc.Status==System.ServiceProcess.ServiceControllerStatus.Paused&&sc.CanPauseAndContinue==true)
   {
    try
    {
     sc.Continue();
    }
    catch(System.Exception ex)
    {
     System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
    }
   }
  }

  private static void StartService(object sender,System.EventArgs e)
  {
   if(sc.Status==System.ServiceProcess.ServiceControllerStatus.Stopped)
   {
    try
    {
     sc.Start();
    }
    catch(System.Exception ex)
    {
     System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
    }
   }
  }

  private static void About(object sender,System.EventArgs e)
  {
   System.Windows.Forms.MessageBox.Show("2005.4.3","关于");
  }

  private static void Exit(object sender,System.EventArgs e)
  {
   try
   {
    timer.Dispose();
    sc.Dispose();
    cm.Dispose();
    ni.Dispose();
    System.Windows.Forms.Application.Exit();
   }
   catch(System.Exception ex)
   {
    System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
   }
  }

  private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   GetServiceState();
  }

  private static void GetServiceState()
  {
   sc.Refresh();
   switch(sc.Status)
   {
    case System.ServiceProcess.ServiceControllerStatus.Stopped:
    {
     ni.Icon=new System.Drawing.Icon("stopped.ico");
     cm.MenuItems[0].Enabled=false;
     cm.MenuItems[1].Enabled=false;
     cm.MenuItems[2].Enabled=false;
     cm.MenuItems[3].Enabled=true;
     break;
    }
    case System.ServiceProcess.ServiceControllerStatus.Running:

    {
     ni.Icon=new System.Drawing.Icon("started.ico");
     cm.MenuItems[0].Enabled=true;
     cm.MenuItems[1].Enabled=true;
     cm.MenuItems[2].Enabled=false;
     cm.MenuItems[3].Enabled=false;
     break;
    }
    case System.ServiceProcess.ServiceControllerStatus.Paused:
    {
     ni.Icon=new System.Drawing.Icon("paused.ico");
     cm.MenuItems[0].Enabled=false;
     cm.MenuItems[1].Enabled=false;
     cm.MenuItems[2].Enabled=true;
     cm.MenuItems[3].Enabled=false;
     break;
    }
    default:
    {
     ni.Icon=new System.Drawing.Icon("paused.ico");
     break;
    }
   }
  }

  private static void SetUpTimer()
  {
   timer=new System.Timers.Timer();
   timer.Interval=500;
   timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
   timer.Start ();
  }
 }
}

Original address: http://blog.csdn.net/rosekisser/article/details/2111731
 
 

Use ASP.NET to control the opening and closing of Windows services

The effect is as shown in the figure


code

The home page needs to add a reference


in the pageload of the page

instantiate windows service

protected void Page_Load(object sender, EventArgs e)
    {
        //The code here is used to display the effect of page loading, which can be ignored.
        ClientScript.RegisterClientScriptBlock(GetType(), "", initJavascript());
        //Instantiate the windows service, LedIPTransfer is the windows service I wrote myself, you can use
        service = new ServiceController("LedIPTransfer");
        OnLoad();
    }

As for obtaining the service name, you can copy the display name after viewing the service details.


Used to display the status of the current service of the page

private void OnLoad()
    {
        //Get the running status of the service
        string state = service.Status.ToString();
        switch (state)
        {
            case "Stopped":
                lblState.Text = "Service has stopped";
                btnRestart.Enabled = false;
                btnStart.Enabled = true;
                btnStop.Enabled = false;
                break;
            case "Running":
                lblState.Text = "Service Running";
                btnStart.Enabled = false;             btnRestart.Enabled
                = true;
                break;
For reference, unless the service starts very slowly, it is generally not necessary to set
            the case "Paused" as follows:
                lblState.Text = "Service is paused";
                break;
            case "StartPending":
                lblState.Text = "Service is starting";
                break;
            case "StopPending":
                lblState.Text = "Service is stopping";
                break;
            case "ContinuePending" :
                lblState.Text = "Service will continue";
                break;
            case "PausePending":
                lblState.Text = "Service will be paused";
                break;
             * */
        }
    }

///<summary>

    /// Start the service

    ///</summary>

    ///<param name="sender"></param>

    ///<param name="e"></param>

    protectedvoid btnStart_Click(object sender,EventArgs e)

    {

        service.Start();

        // Wait for the service to reach the running state

        service.WaitForStatus(ServiceControllerStatus.Running);

        ClientScript.RegisterStartupScript(GetType(),"","window.location.href=window.location.href",true);

    }

    ///<summary>

    /// Stop the service

    ///</summary>

    ///<param name="sender"></param>

    ///<param name="e"></param>

    protectedvoid btnStop_Click(object sender,EventArgs e)

    {

        service.Stop();

        service.WaitForStatus(ServiceControllerStatus.Stopped);

        ClientScript.RegisterStartupScript(GetType(),"","window.location.href=window.location.href",true);

    }

    ///<summary>

    /// Restart the service

    ///</summary>

    ///<param name="sender"></param>

    ///<param name="e"></param>

    protectedvoid btnRestart_Click(object sender,EventArgs e)

    {

        // stop service

        service.Stop();

        service.WaitForStatus(ServiceControllerStatus.Stopped);

        // start the service

        service.Start();

        service.WaitForStatus(ServiceControllerStatus.Running);

        ClientScript.RegisterStartupScript(GetType(),"","window.location.href=window.location.href",true);

    }


The ASP.NET way to open the service requires very high permissions, so if you have insufficient permissions, you cannot open it.

Add identiy under system.web to impersonate a user with administrator privileges

<system.web>

  <identity impersonate="true" userName="用户名" password="密码" />

</system.web>


Original address: http://blog.csdn.net/ysq5202121/article/details/8124185

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325422864&siteId=291194637