Click here to Skip to main content
14,300,872 members
Rate this:
Please Sign up or sign in to vote.
See more:

I have a requirement to get the disk used by the message queue in C# as incoming volume of queue is very high. Is there any way to get MSMQ disk usage.

What I have tried:

I have found way to get number of messages in queue, but nothing on size of queue.
Updated 19-Jul-19 22:31pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

MessageQueue class has a GetAllMessages() which returns an array of Message instances.
Once you get this array, initialize a counter variable to zero. Then loop through all messages in the array, and use the BodyStream property of the Message instance to get a reference to the underlying stream. Finally, use the Length property of the stream to get its length (in bytes), and add it to the counter.
Message[] messages = queue.GetAllMessages();
long total = 0;
foreach (Message message in messages)
   total += message?.BodyStream?.Length;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100