Delete MSMQ Queue During Uninstall

Posted by Todd Kobus on Stack Overflow See other posts from Stack Overflow or by Todd Kobus
Published on 2009-09-14T18:25:14Z Indexed on 2010/12/24 3:54 UTC
Read the original article Hit count: 242

Filed under:
|

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."
            }                
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about msmq