使用 WMI 和 C# 获取 MSMQ 队列

本文关键字:MSMQ 队列 获取 WMI 使用 | 更新日期: 2023-09-27 17:57:07

>我在这里找到了我问题的基本答案:枚举 MSMQ、C# 中的所有传出队列,但是当我尝试运行答案中发布的代码时,在抛出异常"无效查询"之前需要几秒钟到几分钟。

ManagementScope scope = new ManagementScope(@"''"+Environment.MachineName+@"'root'cimv2");
SelectQuery query = new SelectQuery("SELECT * FROM Win32_PerfRawData_MSMQ_MSMQQueue");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
   foreach (var queue in searcher.Get())
   {
   }
}

堆栈跟踪: at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() at TestGetQueueSize.Program.Main(String[] args) in c:''Users''lindj''Documents''Visual Studio 2012''Projects''TestGetQueueSize''TestGetQueueSize''Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

使用 WMI 和 C# 获取 MSMQ 队列

事实证明,

MSMQ 的性能计数器未安装,我打开了一个新问题来处理此问题: 由于缺少性能计数器,MSMQ WMI 查询失败