Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>WMI was the answer! Here's a first pass at the code. It could doubtless be improved.</p> <pre><code>public int GetMessageCount(string queuePath) { const string query = "select * from Win32_PerfRawData_MSMQ_MSMQQueue"; var query = new WqlObjectQuery(query); var searcher = new ManagementObjectSearcher(query); var queues = searcher.Get(); foreach (ManagementObject queue in queues) { var name = queue["Name"].ToString(); if (AreTheSameQueue(queuePath, name)) { // Depending on the machine (32/64-bit), this value is a different type. // Casting directly to UInt64 or UInt32 only works on the relative CPU architecture. // To work around this run-time unknown, convert to string and then parse to int. var countAsString = queue["MessagesInQueue"].ToString(); var messageCount = int.Parse(countAsString); return messageCount; } } return 0; } private static bool AreTheSameQueue(string path1, string path2) { // Tests whether two queue paths are equivalent, accounting for differences // in case and length (if one path was truncated, for example by WMI). string sanitizedPath1 = Sanitize(path1); string sanitizedPath2 = Sanitize(path2); if (sanitizedPath1.Length &gt; sanitizedPath2.Length) { return sanitizedPath1.StartsWith(sanitizedPath2); } if (sanitizedPath1.Length &lt; sanitizedPath2.Length) { return sanitizedPath2.StartsWith(sanitizedPath1); } return sanitizedPath1 == sanitizedPath2; } private static string Sanitize(string queueName) { var machineName = Environment.MachineName.ToLowerInvariant(); return queueName.ToLowerInvariant().Replace(machineName, "."); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload