MessageQueue queue = new MessageQueue(@".\MyQueue"); MessageQueueCriteria criteria = new MessageQueueCriteria(); criteria.Label = "Order*"; criteria.BodyType = BodyType.Xml; MessageEnumerator enumerator = queue.GetMessageEnumerator2(criteria); while (enumerator.MoveNext()) { Message message = enumerator.Current; // Process message here }
using System.Messaging; // Get a list of all accessible public queues string[] publicQueues = MessageQueue.GetPublicQueuesByMachine("."); // Loop through each public queue and display its messages foreach (string queueName in publicQueues) { try { MessageQueue queue = new MessageQueue(queueName); MessageEnumerator enumerator = queue.GetMessageEnumerator2(); while (enumerator.MoveNext()) { Message message = enumerator.Current; // Process message here } } catch (Exception ex) { Console.WriteLine($"Error processing queue: {queueName}. {ex.Message}"); } }In this example, we use the GetPublicQueuesByMachine method to get a list of all accessible public queues on the local machine. We then loop through each public queue, and for each queue, we create an instance of the MessageQueue class and get an enumerator using the GetMessageEnumerator2 method to iterate through all the messages in the queue. If there is any error while processing the queue, we catch the exception and display an error message. The package library for System.Messaging in C# is part of the .NET Framework.