托盘程序如何控制windows服务
来源:互联网 发布:淘宝可以删差评吗 编辑:程序博客网 时间:2024/05/16 04:41
托盘程序通常是一个 单独的程序,所以我们需要自己 建一个单独的 winform 程序!解决方案如下
然后我们需要添加一个 控制 服务进程的类! ClServCtl.cs
源代码 如下!
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using Microsoft.Win32;
namespace WinServicesControl
{
static class ClServCtl
{
/// <summary>
/// 设置服务的启动方式
/// </summary>
/// <param name="key">2自动,3手动,4禁用</param>
/// <returns></returns>
public static bool SetServRunType(string servname, int k)
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + servname, true);
key.SetValue("Start", k);
key.Close();
}
catch { return false; }
return true;
}
/// <summary>
/// 获得服务的启动方式
/// </summary>
/// <returns></returns>
public static string GetServRunType(string servname)
{
string runtype="";
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + servname, true);
runtype = key.GetValue("Start").ToString();
key.Close();
switch (runtype)
{
case"2":
runtype = "自动";
break;
case "3":
runtype = "手动";
break;
case "4":
runtype = "禁用";
break;
}
}
catch { }
return runtype;
}
/// <summary>
/// 把本应用程序添加到系统启动项
/// </summary>
/// <param name="keyName"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool SetAutoRun(string keyName, string filePath)
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
runKey.SetValue(keyName, filePath);
runKey.Close();
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 取消系统启动项
/// </summary>
/// <param name="keyName"></param>
/// <returns></returns>
public static bool DeleteAutoRun(string keyName)
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
//runKey.DeleteSubKey(keyName,true);
runKey.SetValue(keyName, "");
runKey.Close();
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 获取服务的可执行文件路径
/// </summary>
/// <param name="servername"></param>
/// <returns></returns>
public static string GetFilePath(string servername)
{
RegistryKey _Key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Services\" + servername);
if (_Key != null)
{
object _ObjPath = _Key.GetValue("ImagePath");
if (_ObjPath != null)
return _ObjPath.ToString();
}
return "";
}
/// <summary>
/// 获取一个服务的状态
/// </summary>
/// <param name="ServName"></param>
/// <returns></returns>
public static string GetServState(string servername)
{
try
{
ServiceController sc = new ServiceController(servername);
return sc.Status.ToString();
}
catch
{
return "";
}
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="servername"></param>
/// <returns></returns>
public static bool RunService(string servername)
{
try
{
ServiceController sc = new ServiceController(servername);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.Running:
case ServiceControllerStatus.StartPending:
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.PausePending:
case ServiceControllerStatus.ContinuePending:
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Start();
break;
case ServiceControllerStatus.StopPending:
case ServiceControllerStatus.Stopped:
sc.Start();
break;
default: break;
}
sc.WaitForStatus(ServiceControllerStatus.Running);
st = sc.Status;//再次获取服务状态
if (st == ServiceControllerStatus.Running)
{
return true;
}
else
{
return false;
}
}
catch { return false; }
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="servername"></param>
/// <returns></returns>
public static bool StopService(string servername)
{
try
{
ServiceController sc = new ServiceController(servername);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.Running:
case ServiceControllerStatus.StartPending:
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.PausePending:
case ServiceControllerStatus.ContinuePending:
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
break;
}
st = sc.Status;//再次获取服务状态
if (st == ServiceControllerStatus.Stopped)
{
return true;
}
else
{
return false;
}
}
catch { return false; }
}
/// <summary>
/// 暂停服务
/// </summary>
/// <param name="servername"></param>
/// <returns></returns>
public static bool PauseService(string servername)
{
try
{
ServiceController sc = new ServiceController(servername);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.Running:
case ServiceControllerStatus.StartPending:
sc.Pause();
sc.WaitForStatus(ServiceControllerStatus.Paused);
break;
}
st = sc.Status;//再次获取服务状态
if (st == ServiceControllerStatus.Paused)
{
return true;
}
else
{
return false;
}
}
catch { return false; }
}
/// <summary>
/// 恢复暂停的服务
/// </summary>
/// <param name="servername"></param>
/// <returns></returns>
public static bool ResumeService(string servername)
{
try
{
ServiceController sc = new ServiceController(servername);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.PausePending:
sc.Continue();
sc.WaitForStatus(ServiceControllerStatus.Running);
break;
}
st = sc.Status;//再次获取服务状态
if (st == ServiceControllerStatus.Running)
{
return true;
}
else
{
return false;
}
}
catch { return false; }
}
}
}
这个类已经足够我们实现一些常用的右键功能了!然后我们还需要添加一个 winform窗体,还需要加入以下的控件 !
其中contextMenuStript 1需要我们自己配置,如下
下面是 winform 窗体的页面的源代码! 可以参考
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinServicesControl
{
public partial class FrmMian : Form
{
string ServiceName;
Icon icolv;
Icon icohong;
Icon icohuang;
Icon icohui;
public FrmMian()
{
InitializeComponent();
ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"].ToString();
icolv = GetIconImg(0);
icohong = GetIconImg(1);
icohuang = GetIconImg(2);
icohui = GetIconImg(3);
notifyIcon1.Icon = icohui;
notifyIcon1.Text = ServiceName;
启动ToolStripMenuItem.Text = "启动/重启:" + ServiceName;
停止ToolStripMenuItem.Text = "停止:" + ServiceName;
//暂停ToolStripMenuItem.Text = "暂停:" + ServiceName;
//恢复暂停ToolStripMenuItem.Text = "恢复暂停:" + ServiceName;
}
private Icon GetIconImg(int index)
{
Image img = imageList1.Images[index];
Bitmap b = new Bitmap(img);
Icon icon = Icon.FromHandle(b.GetHicon());
return icon;
}
private void timer1_Tick(object sender, EventArgs e)
{
string ServState = ClServCtl.GetServState(ServiceName);
string RunType = ClServCtl.GetServRunType(ServiceName);
switch (ServState)
{
case""://没有服务
notifyIcon1.Icon = icohui ;
notifyIcon1.Text = "没有找到服务:" + ServiceName;
break;
case "Stopped"://停止
notifyIcon1.Icon = icohong ;
notifyIcon1.Text = ServiceName + " 服务已停止("+ RunType +")";
break;
case "Running"://运行
notifyIcon1.Icon = icolv ;
notifyIcon1.Text = ServiceName + " 服务正在运行(" + RunType + ")";
break;
case "StartPending"://正在启动
notifyIcon1.Icon = icolv;
notifyIcon1.Text = ServiceName + " 服务正在尝试启动(" + RunType + ")";
break;
case "StopPending"://正在停止
notifyIcon1.Icon = icohong;
notifyIcon1.Text = ServiceName + " 服务正在尝试停止(" + RunType + ")";
break;
case "Paused":
notifyIcon1.Icon = icohong;
notifyIcon1.Text = ServiceName + " 服务已暂停(" + RunType + ")";
break;
case "PausePending":
notifyIcon1.Icon = icohong;
notifyIcon1.Text = ServiceName + " 服务正在尝试暂停(" + RunType + ")";
break;
case "ContinuePending":
notifyIcon1.Icon = icohong;
notifyIcon1.Text = ServiceName + " 服务正在尝试恢复运行(" + RunType + ")";
break;
}
}
private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!ClServCtl.RunService(ServiceName))
{
MessageBox.Show("启动/重启操作失败");
}
}
private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!ClServCtl.StopService(ServiceName))
{
MessageBox.Show("停止操作失败");
}
}
//private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
//{
// if (!ClServCtl.PauseService(ServiceName))
// {
// MessageBox.Show("暂停操作失败");
// }
//}
//private void 恢复暂停ToolStripMenuItem_Click(object sender, EventArgs e)
//{
// if (!ClServCtl.ResumeService(ServiceName))
// {
// MessageBox.Show("恢复暂停操作失败");
// }
//}
private void 添加到系统启动项ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ClServCtl.SetAutoRun("AutoRun" + ServiceName, Application.ExecutablePath))
{
MessageBox.Show("操作成功");
}
else
{
MessageBox.Show("操作失败");
}
}
private void 取消系统启动项ToolStripMenuItem_Click(object sender, EventArgs e)
{
ClServCtl.DeleteAutoRun("AutoRun" + ServiceName);
}
private void 打开执行文件目录ToolStripMenuItem_Click(object sender, EventArgs e)
{
string filepath = "";
string filename = ClServCtl.GetFilePath(ServiceName);
if (filename == "")
{
MessageBox.Show("操作失败,没有找到路径");
return;
}
for (int i = filename.Length - 1; i > 0; i--)
{
if (filename.Substring(i, 1) == @"\")
{
filepath = filename.Substring(0, i);
break;
}
}
try
{
System.Diagnostics.Process.Start("explorer.exe", filepath);
}
catch
{
MessageBox.Show("操作失败,没有找到合法路径");
}
}
private void 自动ToolStripMenuItem_Click(object sender, EventArgs e)
{
ClServCtl.SetServRunType(ServiceName, 2);
}
private void 手动ToolStripMenuItem_Click(object sender, EventArgs e)
{
ClServCtl.SetServRunType(ServiceName, 3);
}
private void 禁用ToolStripMenuItem_Click(object sender, EventArgs e)
{
ClServCtl.SetServRunType(ServiceName, 4);
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
最后就是配置我们的 config 文件了!
value 是我们的服务名! key 值是我们在程序里使用的名称!
这些都做完后,就可以 联系到我们在上一篇中写好的 服务程序了!
- 托盘程序如何控制windows服务
- 托盘程序如何控制windows服务
- 如何控制Windows服务程序
- delphi编写windows服务程序,含托盘
- 用托盘控制windows服务的c#实现
- 摘:用托盘控制windows服务的c#实现
- 用托盘控制windows服务的c#实现
- 用托盘控制windows服务的c#实现 (转)
- C#实现用托盘控制windows服务的
- windows控制台下托盘
- windows服务+托盘控制程序
- Windows托盘程序实现
- 如何编写 Windows 服务程序
- 如何编写windows服务程序
- 如何编写windows服务程序
- 如何编写windows服务程序
- 如何编写windows服务程序
- c# 做 windows 托盘程序
- Android 的系统属性(SystemProperties)设置分析
- @Scope("prototype")其实很重要
- [笔记]Sensor概述
- windows 服务与系统托盘之间的交互
- 10879 - Code Refactoring
- 托盘程序如何控制windows服务
- HighChartS cpu利用率动态图(Java版)
- [网络编程]Win32 Socket+OpenSSL实例
- hdu1257 最少拦截系统 (贪心)
- 10250 - The Other Two Trees
- 在windows服务里如何使用timer
- Openfire JSP Plugin 开发笔记
- lsyncd+rsync实时同步rsync同步
- Android Framework ActivityManagerService(1)