如何以编程方式知道计算机上是否启用了消息队列? [英] How to know programmatically whether Message Queueing is enabled on the machine or not?
问题描述
我知道当我尝试创建新的MessageQueue
时,如果未启用消息队列,系统将抛出InvalidOperationException
.
I know that when I try to create new MessageQueue
, system throws InvalidOperationException
if the Message Queuing is not enabled.
但是如何以编程方式知道计算机上是否启用了消息队列? 我正在使用C#2.0&两种不同代码库中的C#4.0.
But how to know programmatically whether Message Queueing is enabled on the machine or not? I am using C# 2.0 & C# 4.0 in two different code bases.
推荐答案
您可以为此使用 System.ServiceProcess ,但是首先您需要为项目添加引用Service.ServiceProcess
,您可以检索所有服务并按以下方式获取其状态:
You can use the System.ServiceProcess for this one, but first you need to add reference to your project the Service.ServiceProcess
, and you can retrieve all the services and get their status like this:
List<ServiceController> services = ServiceController.GetServices().ToList();
ServiceController msQue = services.Find(o => o.ServiceName == "MSMQ");
if (msQue != null) {
if (msQue.Status == ServiceControllerStatus.Running) {
// It is running.
}
} else { // Not installed? }
这篇关于如何以编程方式知道计算机上是否启用了消息队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!