The WriteTextToQueue and WriteDSToQueue methods are functionally similar. In fact with a small amount of code modification they could be combined into a parameterized single method. You will notice that I make constant use of 'out' parameters in functions. It is my preference to make the output of most methods a Boolean indicating success or failure. The functionality of the tree view is fairly straight forward, but there is one point to note.
When the tree is populated, the nodes are listed as the Name (Label) of the Message, and the Tag field is used to denote whether the node is a Queue or a Message. What is not gathered and used in the tree view is the Id of the message. This is very important and after I reached the point where I wanted to use the PeekByID method I found that I had to find the Id of the message I wanted to work with. I considered this and decided that I would reiterate the messages in an individual queue and match the Label with the desired Message Id. You will find that the MessagePeek and RefreshTree methods both contain examples of iterating messages, differing only in the choice of objects used to do so. I suggest that you review each method to find which works best for you.
One point to take particular note of is the Path used to work with a queue. Notice that since I am working with private queues, the Path is in the form of ' .\private$\<queuename> '. Be careful not forget to include the leading '.\' in the string. Depending on how you pull the sting, you may or may not retrieve the leading '.\'. Also, because we are using C#, and you will want to list the characters in this sting as either '.\\private$\\<queuename>' or '@".\private$\<queuename>"'. If you run into problems where the error states that the queue name is not listed in the Directory Service (DS), check to make sure that the queue path is properly escaped and prefixed. You will notice that I use the line 'MessageQueue mq = new MessageQueue( ".\\" + Path ); ' in the MessagePeek method. Upon receiving errors at one point, I noticed that the prefix was missing and decided to correct it in this fashion because I did not want to take the time to track down why it was happening here when it was not happening in other places. This is never a good practice in production development, but it provided a memorable example of this problem and one way to fix it.
Up until this point I have glossed over most of the functionality, knowing that you can easily find what you need by stepping thru the code, but I will now focus on some of the more interesting details.
As I described earlier, I had to iterate the messages to find the Message Id for the message I wanted to retrieve. You will find that code in the MessagePeek method. Once I found the Id, and used it locate the message I wanted, I had to cast the body of the message in the format that was required to use the data. When you create an object to contain the data in the body of the message, you must first create a message queue formatter. This is used to make sure that if you Send a DataSet, you receive a DataSet. When you create the message formatter, you need to add the object types that the formatter will require to properly assist you in using the contents of the message.
As another side note, while I do not go into detail in this application, the formatter can be either an XML or Binary formatter. This example application uses the XML version of the message queue formatter.