一.引子
对某一个服务采用类似SQL Server2000的服务管理器的方式来进行管理,包括带参数启动,暂停,停止,设置自动运行等操作。这里的重点不是建立服务,而是控制与操作服务,所以对服务的建立与注册不做叙述。
二.运行时效果
三.主要实现代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.ServiceProcess;
- using System.Threading;
- namespace ReportServiceManager
- {
- public partial class FrmServer : Form
- {
- private bool _isClose = false;
- public FrmServer()
- {
- InitializeComponent();
- }
- private void FrmServer_Load(object sender, EventArgs e)
- {
- ReadConfigXML();
- InitServiceStatus();
- this.txtServer.Text = PublicUse.ReportServer;
- this.txtService.Text = PublicUse.ReportServiceName;
- this.FormClosing += new FormClosingEventHandler(FrmServer_FormClosing);
- //this.Resize += new EventHandler(FrmServer_Resize);
- }
- void FrmServer_Resize(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- this.Hide();
- }
- }
- void FrmServer_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (!_isClose)
- {
- string value = "3";
- if (this.cbxIsautorun.Checked)
- {
- value = "2";
- }
- setReg_Services(PublicUse.ReportServiceName, "Start", value);
- e.Cancel = true;
- this.WindowState = FormWindowState.Minimized;
- }
- else
- {
- this.notifyIcon1.Visible = false;
- this.notifyIcon1.Dispose();
- }
- }
- //读取配置文件
- private void ReadConfigXML()
- {
- PublicUse.SetSecret(Application.StartupPath + @"/AppSet.XML");
- }
- private void InitServiceStatus()
- {
- string server = PublicUse.ReportServer;
- string service = PublicUse.ReportServiceName;
- try
- {
- if ( server!= "" && service!= "")
- {
- ServiceController serviceCol = new ServiceController(service, server);
- if (serviceCol.Status == ServiceControllerStatus.Running)
- {
- btnStart.Enabled = false;
- btnStop.Enabled = true;
- btnPause.Enabled = true;
- this.tsbStatus.Text = "正在运行--//" + server + "-" + service;
- }
- if (serviceCol.Status == ServiceControllerStatus.Stopped)
- {
- btnStart.Enabled = true;
- btnStop.Enabled = false;
- btnPause.Enabled = false;
- this.tsbStatus.Text = "已经停止--//" + server + "-" + service;
- }
- if (serviceCol.Status == ServiceControllerStatus.Paused)
- {
- btnStart.Enabled = true;
- btnStop.Enabled = true;
- btnPause.Enabled = false;
- this.tsbStatus.Text = "已经暂停--//" + server + "-" + service;
- }
- }
- else
- {
- MessageBox.Show("请检查配置文件的服务器设置!", "错误");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- string server = this.txtServer.Text.Trim();
- string service = this.txtService.Text.Trim();
- try
- {
- if (server != "" && service != "")
- {
- ServiceController serviceCol = new ServiceController(service, server);
- if (serviceCol.Status == ServiceControllerStatus.Stopped)
- {
- this.tsbStatus.Text = "正在启动--//" + server + "-" + service;
- //获取参数
- string datestr = PublicUse.GetXmlValue(Application.StartupPath + @"/AppSet.XML", "begindate");
- string cyclen = PublicUse.GetXmlValue(Application.StartupPath + @"/AppSet.XML", "cyclength");
- string cyctime = PublicUse.GetXmlValue(Application.StartupPath + @"/AppSet.XML", "cycletime");
- string[] args = new string[]{datestr,cyctime,cyclen};
- serviceCol.Start(args);
- while (serviceCol.Status == ServiceControllerStatus.Stopped)
- {
- Thread.Sleep(1000);
- serviceCol.Refresh();
- }
- this.tsbStatus.Text = "正在运行--//" + server + "-" + service;
- this.btnStart.Enabled = false;
- this.btnPause.Enabled = true;
- this.btnStop.Enabled = true;
- }
- if (serviceCol.Status == ServiceControllerStatus.Paused)
- {
- this.tsbStatus.Text = "正在继续--//" + server + "-" + service;
- if (this.cbxIsautorun.Checked)
- {
- }
- serviceCol.Continue();
- while (serviceCol.Status == ServiceControllerStatus.Paused)
- {
- Thread.Sleep(1000);
- serviceCol.Refresh();
- }
- this.tsbStatus.Text = "正在运行--//" + server + "-" + service;
- this.btnStart.Enabled = false;
- this.btnPause.Enabled = true;
- this.btnStop.Enabled = true;
- }
- }
- else
- {
- MessageBox.Show("请检查配置文件的服务器设置!", "错误");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void btnPause_Click(object sender, EventArgs e)
- {
- string server = this.txtServer.Text.Trim();
- string service = this.txtService.Text.Trim();
- if (server != "" && service != "")
- {
- ServiceController serviceCol = new ServiceController(service, server);
- serviceCol.Pause();
- this.tsbStatus.Text = "正在暂停--//" + server + "-" + service;
- while (serviceCol.Status != ServiceControllerStatus.Paused)
- {
- Thread.Sleep(1000);
- serviceCol.Refresh();
- }
- this.tsbStatus.Text = "已经暂停--//" + server + "-" + service;
- this.btnPause.Enabled = false;
- this.btnStart.Enabled = true;
- this.btnStop.Enabled = true;
- }
- else
- {
- MessageBox.Show("请检查配置文件的服务器设置!", "错误");
- }
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- string server = this.txtServer.Text.Trim();
- string service = this.txtService.Text.Trim();
- if (server != "" && service != "")
- {
- ServiceController serviceCol = new ServiceController(service, server);
- serviceCol.Stop();
- this.tsbStatus.Text = "正在停止--//" + server + "-" + service;
- while (serviceCol.Status != ServiceControllerStatus.Stopped)
- {
- Thread.Sleep(1000);
- serviceCol.Refresh();
- }
- this.tsbStatus.Text = "已经停止--//" + server + "-" + service;
- this.btnStop.Enabled = false;
- this.btnStart.Enabled = true;
- this.btnPause.Enabled = false;
- }
- else
- {
- MessageBox.Show("请检查配置文件的服务器设置!", "错误");
- }
- }
- private void btnRefresh_Click(object sender, EventArgs e)
- {
- string server = this.txtServer.Text.Trim();
- string service = this.txtService.Text.Trim();
- if (server != "" && service != "")
- {
- ServiceController serviceCol = new ServiceController(service, server);
- serviceCol.Refresh();
- Thread.Sleep(1000);
- }
- else
- {
- MessageBox.Show("请检查配置文件的服务器设置!", "错误");
- }
- }
- private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- this.WindowState = FormWindowState.Normal;
- }
- this.Activate();
- }
- #region----------注册表操作----------
- //获得服务注册表键值
- public static string getReg_Services(string serverName)
- {
- string rootKey = "HKEY_LOCAL_MACHINE";
- string subKey = "SYSTEM//CurrentControlSet//Services//" + serverName;//你的服务名;
- string keyname = rootKey + "//" + subKey;
- return getReg(keyname, "Start");
- }
- //写入服务注册表键值
- private static void setReg_Services(string serverName,string keyValue, string newvalue)
- {
- string rootKey = "HKEY_LOCAL_MACHINE";
- string subKey = "SYSTEM//CurrentControlSet//Services//" + serverName;//你的服务名;
- string keyname = rootKey + "//" + subKey;
- setReg(keyname, keyValue, newvalue);//先得查看其值
- }
- private static string getReg(string keyname, string valuename)
- {
- //string valuename = "";
- object defaultvalue = "";
- return Microsoft.Win32.Registry.GetValue(keyname, valuename, defaultvalue).ToString();
- }
- private static void setReg(string keyName, string valuename, string keyvalue)
- {
- Microsoft.Win32.Registry.SetValue(keyName, valuename, keyvalue, Microsoft.Win32.RegistryValueKind.DWord);
- }
- #endregion--------------------------
- #region ---------菜单操作----------
- private void tmiExit_Click(object sender, EventArgs e)
- {
- _isClose = true;
- this.Close();
- }
- private void tmiOpen_Click(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- this.WindowState = FormWindowState.Normal;
- this.Activate();
- }
- private void tmiSet_Click(object sender, EventArgs e)
- {
- FrmSet frm = new FrmSet();
- frm.ShowDialog();
- if (frm.IsButtonOK)
- {
- //停止服务
- this.btnStop.PerformClick();
- //重新加载
- ReadConfigXML();
- InitServiceStatus();
- this.txtServer.Text = PublicUse.ReportServer;
- this.txtService.Text = PublicUse.ReportServiceName;
- }
- }
- private void tmiStart_Click(object sender, EventArgs e)
- {
- this.btnStart.PerformClick();
- }
- private void tmiPause_Click(object sender, EventArgs e)
- {
- this.btnPause.PerformClick();
- }
- private void tmiStop_Click(object sender, EventArgs e)
- {
- btnStop.PerformClick();
- }
- private FrmAbout _frmAbout;
- private void tmiAbout_Click(object sender, EventArgs e)
- {
- if (_frmAbout == null || _frmAbout.IsDisposed)
- {
- _frmAbout = new FrmAbout();
- }
- _frmAbout.Show();
- _frmAbout.Activate();
- }
- #endregion--------------------------
- }
- }