programmatically add private queues in MSMQ
Asked Answered
E

3

11

I have list of over 20 queues that needs to be added as private queue in MSMQ.

Is there a way to do it using

  1. Command Line

  2. C# programming

If there is a way to do using some sort of script or .net programming then I could add it with out manually inputting it and causing typos.

Please let me know.

thanks

Easement answered 5/4, 2011 at 22:3 Comment(0)
K
19
using System.Messaging;

//...

void CreateQueue(string qname) {
   if (!MessageQueue.Exists(qname)) MessageQueue.Create(qname);
}

You can only create private queues on your local computer. For more information see: Creating Queues

Knecht answered 5/4, 2011 at 22:12 Comment(7)
Richard, do you know how to set the permissions programatically as well in C#?Easement
Use the AccessControlList to setup the permissions and then "queue.SetPermissions()". Note that MessageQueue.Create will return the queue.Knecht
I found this article helpful regarding AccessControlList serializer.blogspot.com/2005/12/…Rezzani
Hi, you can create public queues.Naidanaiditch
I would never approve the edit by @GregoryCurrie. It should just be using System.Messaging. I've changed the edit. I only do using ... = ... when I have name clashes.Knecht
@Richard Schneider All good. The important thing is the assembly is mentioned.Lodging
@GregroyCurrie, I've been on SO for a few years now and its not standard practice to mention assemblies that can be assumed from the OP. However, I think for newbies its a good idea. Perhaps you should mention this to the higher gods on Meta?Knecht
R
2

For command line, you can create a .vbs file with following content:

Option Explicit

Dim objInfo
Dim objQue
Dim objMsg
Dim strFormatName   ' Destination

strFormatName = "direct=os:.\private$\test"

Set objInfo = CreateObject("MSMQ.MSMQQueueInfo")
Set objMsg = CreateObject("MSMQ.MSMQMessage")

objMsg.Label = "my message"
objMsg.Body = "This is a sample message."
objInfo.FormatName = strFormatName
set objQue = objInfo.Open( 2, 0 )

' Send Message
objMsg.Send  objQue

' Close Destination
objQue.Close

Set objMsg = Nothing
Set objInfo = Nothing

msgbox "Done..."
Rundell answered 17/1, 2012 at 19:41 Comment(0)
N
0

A bit late on this, however I only started working on them now.

To add to Richard's answer, you can create public queues. you need the hostname though and admin access to that machine.

  public static MessageQueue CreatePrivate(string name) {
        string path = string.Format(@".\private$\{0}", name);
        if (!MessageQueue.Exists(path)) {
            MessageQueue.Create(path);
            return new MessageQueue(path);
        }
        return new MessageQueue(path);
    }

    public static MessageQueue CreatePublic(string hostname,string queuename) {
        string path = string.Format(@"{0}\{1}", hostname,queuename);
        if (!MessageQueue.Exists(path)) {
            MessageQueue.Create(path);
            return new MessageQueue(path);
        }
        return new MessageQueue(path);
    }
}
Naidanaiditch answered 20/11, 2015 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.