Determines whether a queue with the specified path exists.
[Visual Basic] Public Shared Function Exists( _ ByVal path As String _ ) As Boolean [C#] public static bool Exists( string path ); [C++] public: static bool Exists( String* path ); [JScript] public static function Exists( path : String ) : Boolean;
true if a queue with the specified path exists; otherwise, false.
Exception Type | Condition |
---|---|
ArgumentNullException | The path parameter is a null reference (in Visual Basic Nothing) or an empty string. |
MessageQueueException | The syntax for the path parameter is invalid. |
InvalidOperationException | The queue's format name generated an exception. |
This method can be used to determine whether a Message Queuing queue resource with the specified path exists. There is no way to determine whether a queue with a specified format name exists. If you do not verify the existence of a queue before sending a message, the application throws an exception if the queue is not found. Because the MachineName can be given as part of the Path, the Exists method can be used to search for paths on any computer on the network.
The syntax for the path parameter depends on the type of queue it references. The following table shows the syntax you should use for a queue of a specified type.
Queue Type | Syntax |
---|---|
Public Queue | MachineName\QueueName |
Private Queue | MachineName\Private$\QueueName |
You can also use the Label of a Message Queuing resource to describe the queue path. The following table shows the proper syntax for that type of reference.
Reference | Syntax |
---|---|
Label | Label:[label] |
The following example verifies whether a queue named "myQueue" exists on the local computer. If it does not, the queue is created. Then, a message is sent to the queue.
[Visual Basic]
Private Sub CreateNewQueue() 'Define a new MessageQueue object Dim messageQueue1 As MessageQueue Dim queuePath As String queuePath = ".\myQueue" 'If no queue exists at the specified path, create one. If Not MessageQueue.Exists(queuePath) Then messageQueue1 = MessageQueue.Create(queuePath) Else 'Set the MessageQueue to the existing path. messageQueue1 = New MessageQueue(queuePath) End If 'Send a message to the queue messageQueue1.Send("Test message.") End Sub
MessageQueue Class | MessageQueue Members | System.Messaging Namespace