Delete MSMQ Queue During Uninstall
- by Todd Kobus
Is it possible to delete a private message queue that was created by the service user? During uninstallation, we would like to clean up any message queues created by our application. For security purposes, access to these queues has been restricted to the current user (ServiceUser). During uninstall, we have admin privileges, but still get an access denied MessageQueueException when we attempt to delete the queue or modify the privs on the queue.
Here is the cleanup code:
public void DeleteAppQueues()
{
List<string> trash = new List<string>();
var machineQueues = MessageQueue.GetPrivateQueuesByMachine(".");
foreach (var q in machineQueues)
{
if (IsAppQueue(q.QueueName))
{
trash.Add(".\\" + q.QueueName);
}
q.Dispose();
}
foreach (var queueName in trash)
{
try
{
using (MessageQueue delQueue = new MessageQueue(queueName))
{
delQueue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
}
MessageQueue.Delete(queueName);
}
catch (MessageQueueException ex)
{
// ex.Message is "Access to Message Queuing system is denied."
}
}
}