C#实现用托盘控制windows服务的

此示例代码展示了如何使用C#创建一个系统托盘应用程序,该程序可以控制多个Windows服务的状态,包括启动、停止、暂停和继续。通过定时器检查服务状态,并更新托盘图标以反映服务状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个例子既实现了系统托盘功能,也实现了多windows服务的控制,非常实用!

using System;

namespace UBISerialsController
{
 /// <summary>
 /// 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: 在此处添加构造函数逻辑
   //
  
  }

  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("

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值