Share via


MessageQueue.Exists(String) 方法

定義

判斷指定路徑上是否存在訊息佇列。

public:
 static bool Exists(System::String ^ path);
public static bool Exists (string path);
static member Exists : string -> bool
Public Shared Function Exists (path As String) As Boolean

參數

path
String

要尋找的佇列位置。

傳回

如果指定路徑上的佇列存在則為 true,否則為 false

例外狀況

path 語法無效。

存取訊息佇列方法時發生錯誤。

-或-

Exists(String) 方法會在遠端私用佇列上呼叫。

確認佇列是否存在時,應用程式會使用格式名稱語法。

範例

下列程式代碼範例會驗證消息佇列佇列是否存在,然後加以刪除。

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
int main()
{
   
   // Determine whether the queue exists.
   if ( MessageQueue::Exists( ".\\myQueue" ) )
   {
      try
      {
         
         // Delete the queue.
         MessageQueue::Delete( ".\\myQueue" );
      }
      catch ( MessageQueueException^ e ) 
      {
         if ( e->MessageQueueErrorCode == MessageQueueErrorCode::AccessDenied )
         {
            Console::WriteLine( "Access is denied. Queue might be a system queue." );
         }
         
         // Handle other sources of MessageQueueException.
      }

   }

   return 0;
}
using System;
using System.Messaging;

namespace MyProject
{
    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //		
        // This example verifies existence and attempts to
        // delete a queue.
        //**************************************************

        public static void Main()
        {

            // Determine whether the queue exists.
            if (MessageQueue.Exists(".\\myQueue"))
            {
                try
                {
                    // Delete the queue.
                    MessageQueue.Delete(".\\myQueue");
                }
                catch(MessageQueueException e)
                {
                    if(e.MessageQueueErrorCode ==
                        MessageQueueErrorCode.AccessDenied)
                    {
                        Console.WriteLine("Access is denied. " +
                            "Queue might be a system queue.");
                    }

                    // Handle other sources of MessageQueueException.
                }
            }
        
            return;
        }
    }
}
Imports System.Messaging



Public Class MyNewQueue


        
        ' Provides an entry point into the application.
        '		 
        ' This example verifies existence and attempts to 
        ' delete a queue.
        

        Public Shared Sub Main()

            ' Determine whether the queue exists.
            If MessageQueue.Exists(".\myQueue") Then

                Try

                    ' Delete the queue.
                    MessageQueue.Delete(".\myQueue")

                Catch e As MessageQueueException

                    If e.MessageQueueErrorCode = _
                        MessageQueueErrorCode.AccessDenied Then

                        Console.WriteLine("Access is denied. " _
                            + "Queue might be a system queue.")
                    End If

                    ' Handle other sources of exceptions as necessary.

                End Try

            End If


            Return

        End Sub

End Class

備註

方法 Exists(String) 會判斷消息佇列佇列是否存在於指定的路徑。 沒有方法可用來判斷具有指定格式名稱的佇列是否存在。 如需格式名稱語法和其他路徑語法窗體的詳細資訊,請參閱 Path property.)

Exists(String) 是昂貴的作業。 只有在應用程式內需要時才使用它。

注意

方法 Exists(String) 不支援前置 FormatName 詞。

參數的 path 語法取決於佇列的類型,如下表所示。

佇列類型 語法
公用佇列 MachineName\QueueName

Exists(String) 無法呼叫 以確認遠端私人佇列是否存在。

如需更多語法,請參閱 Path 屬性。

或者,您可以使用 Label 來描述佇列路徑。

參考 語法
標籤 Label:[ label ]

下表顯示此方法是否可在各種工作組模式中使用。

工作組模式 可用
本機電腦
本機計算機和直接格式名稱
遠端電腦
遠端電腦和直接格式名稱

適用於

另請參閱