如何 查找window service 中的服务[50分,解决问题后,马上给分]
我现在需要做这样一个功能:我知道服务的名称,我想通过程序去查找,这个服务有没有注册,如果没有注册,再去注册服务(.NET 1.1)
先谢各位,回复者必有分。谢谢.. --------------------编程问答-------------------- up --------------------编程问答-------------------- 不管有没存在,都进行注册,然后自定义捕抓'服务已存在'的错误处理,不中止程序运行就OK. --------------------编程问答--------------------
System.ServiceProcess.ServiceController[] services;--------------------编程问答-------------------- using System;
services = ServiceController.GetServices();
protected void SetServiceStatus(ServiceController controller)
{
buttonStart.Enabled = true;
buttonStop.Enabled = true;
buttonPause.Enabled = true;
buttonContinue.Enabled = true;
if (!controller.CanPauseAndContinue)
{
buttonPause.Enabled = false;
buttonContinue.Enabled = false;
}
if (!controller.CanStop)
{
buttonStop.Enabled = false;
}
ServiceControllerStatus status = controller.Status;
switch (status)
{
case ServiceControllerStatus.ContinuePending:
1119
Windows Services
textServiceStatus.Text = “Continue Pending”;
buttonContinue.Enabled = false;
break;
case ServiceControllerStatus.Paused:
textServiceStatus.Text = “Paused”;
buttonPause.Enabled = false;
buttonStart.Enabled = false;
break;
case ServiceControllerStatus.PausePending:
textServiceStatus.Text = “Pause Pending”;
buttonPause.Enabled = false;
buttonStart.Enabled = false;
break;
case ServiceControllerStatus.StartPending:
textServiceStatus.Text = “Start Pending”;
buttonStart.Enabled = false;
break;
case ServiceControllerStatus.Running:
textServiceStatus.Text = “Running”;
buttonStart.Enabled = false;
buttonContinue.Enabled = false;
break;
case ServiceControllerStatus.Stopped:
textServiceStatus.Text = “Stopped”;
buttonStop.Enabled = false;
break;
case ServiceControllerStatus.StopPending:
textServiceStatus.Text = “Stop Pending”;
buttonStop.Enabled = false;
break;
default:
textServiceStatus.Text = “Unknown status”;
break;
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Windows.Forms;
using System.Management;
namespace SurveyorLayerControl
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);
this.BeforeInstall += new InstallEventHandler(ProjectInstaller_BeforeInstall);
this.serviceInstaller1.ServiceName = "MyService";
this.serviceInstaller1.DisplayName = "Dummy Service";
}
void ProjectInstaller_BeforeInstall(object sender, InstallEventArgs e)
{
//check for required the Services.
//ServiceController[] sc = ServiceController.GetServices();
//bool Required_Services_Found = false;
//for (int i = 0; i < sc.Length; i++)
//{
// if (sc[i].ServiceName.ToLower() == "myservice")
//{
// Required_Services_Found = true;
// break;
// }
//}
//
//Required_Services_Found = true; //just for testing purposed. this will be removed.
//
ServiceController sc = new ServiceController(this.serviceInstaller1.ServiceName);
//如果你要找的是你这个相同的服务的话, 那this.serviceInstaller1.ServiceName就应该是你的windows service的名字了。
//注意DisplayName and ServiceName是不同的. 在你的代码里,如果你在某些地方更改了ServiceName,那上面的这一行代码就找不到你的service,
//你也可以用:ServiceController sc = new ServiceController("服务名称");
if (sc != null) //如果不是null,那这个服务就是存在的了。
{
MessageBox.Show("对不起,\n在您的系统中已经安装有本服务,请按确定退出安装.......", "安装错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
}
void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
//Starts up the Service.
ServiceController sc = new ServiceController(this.serviceInstaller1.ServiceName);
if (sc != null)
{
//sc.Start();
//
//sc.WaitForStatus(ServiceControllerStatus.Running);
//MakeServiceInteractive();
}
}
}
} --------------------编程问答-------------------- 兄弟们,System.ServiceProcess and System.Management,在.net 1.1下没有呀。
有没有谁给给出.NET 1.1下的解决方案??? --------------------编程问答-------------------- 谁说1.1没有 System.ServiceProcess?
你要自己添加reference. 默认是没有的, 因为这个namespace不是在mscorlib里。 --------------------编程问答-------------------- .NET 1.1: http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(VS.71).aspx
.NET 2.0: http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(VS.80).aspx
.NET 3.0: http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(VS.85).aspx
.NET 3.5: http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx
补充:.NET技术 , C#