```csharp
public static System.Messaging.MessageQueue Create(string path, bool transactional)
{
return MessageQueue.Create(path, transactional);
}
public static System.Messaging.MessageQueue Create(string path)
{
try
{
return Create(path, false);
}
catch (Exception ex)
{
throw new Exception("队列已存在于指定的路径中。", ex);
}
}
```
```csharp
using System;
using System.Messaging;
namespace MyNewQueueNamespace
{
public class MyNewQueue
{
public void CreatePublicTransactionalQueues()
{
// Create and connect to a public message Queuing queue.
if (!MessageQueue.Exists(".\\newPublicTransQueue1"))
{
// Create the queue if it does not exist.
MessageQueue.Create(".\\newPublicTransQueue1", true);
}
// Connect to the queue.
MessageQueue^ myNewPublicQueue = gcnew MessageQueue(".\\newPublicTransQueue1");
// Create a transaction.
MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction;
// Begin the transaction.
myTransaction->Begin();
// Send the message.
myNewPublicQueue->Send("My Message Data.", myTransaction);
// Commit the transaction.
myTransaction->Commit();
if (!MessageQueue.Exists(".\\newPublicTransQueue2"))
{
// Create (but do not connect to) second public queue
MessageQueue.Create(".\\newPublicTransQueue2", true);
}
}
public void CreatePrivateTransactionalQueues()
{
// Create and connect to a private Message Queuing queue.
if (!MessageQueue.Exists(".\\Private$\\newPrivTransQ1"))
{
// Create the queue if it does not exist.
MessageQueue^ myNewPrivateQueue = MessageQueue::Create(".\\Private$\\newPrivTransQ1", true);
}
// Connect to the queue.
MessageQueue^ myNewPrivateQueue = gcnew MessageQueue(".\\Private$\\newPrivTransQ1");
// Create a transaction.
MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction;
// Begin the transaction.
myTransaction->Begin();
// Send the message.
myNewPrivateQueue->Send("My Message Data.", myTransaction);
// Commit the transaction.
myTransaction->Commit();
// Create (but do not connect to) a second private queue.
if (!MessageQueue.Exists(".\\Private$\\newPrivTransQ2"))
{
MessageQueue::Create(".\\Private$\\newPrivTransQ2", true);
}
}
}
}
```
```csharp
using System;
using System.Messaging;
namespace MyProject
{
///
/// Provides a container class for the example.
///
public class MyNewQueue
{
//**************************************************
// Provides an entry point into the application.
// // This example creates new transactional queues.
//**************************************************
public static void Main()
{
// Create a new instance of the class.
MyNewQueue myNewQueue = new MyNewQueue();
// Create transactional queues.
myNewQueue.CreatePublicTransactionalQueues();
myNewQueue.CreatePrivateTransactionalQueues();
return;
}
//**************************************************
// Creates public transactional queues and sends a message.
//**************************************************
public void CreatePublicTransactionalQueues()
{
// Create and connect to a public Message Queuing queue.
if (!MessageQueue.Exists(".\\newPublicTransQueue1"))
{
// Create the queue if it does not exist.
MessageQueue.Create(".\\newPublicTransQueue1", true);
}
// Connect to the queue.
MessageQueue myNewPublicQueue = new MessageQueue(".\\newPublicTransQueue1");
// Send a message to the queue.
// Create a transaction.
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
// Begin the transaction.
myTransaction.Begin();
// Send the message.
myNewPublicQueue.Send("My Message Data.", myTransaction);
// Commit the transaction.
myTransaction.Commit();
if (!MessageQueue.Exists(".\\newPublicTransQueue2"))
{
// Create (but do not connect to) second public queue.
MessageQueue.Create(".\\newPublicTransQueue2", true);
}
return;
}
//*************************************************
// Creates private queues and sends a message.
//*************************************************
public void CreatePrivateTransactionalQueues()
{
// Create and connect to a private Message Queuing queue.
if (!MessageQueue.Exists(".\\Private$\\newPrivTransQ1"))
{
// Create the queue if it does not exist.
MessageQueue.Create(".\\Private$\\newPrivTransQ1", true);
}
// Connect to the queue.
MessageQueue myNewPrivateQueue = new MessageQueue(".\\Private$\\newPrivTransQ1");
// Send a message to the queue.
// Create a transaction.
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
// Begin the transaction.
myTransaction.Begin();
// Send the message.
myNewPrivateQueue.Send("My Message Data.", myTransaction);
// Commit the transaction.
myTransaction.Commit();
// Create (but do not connect to) a second private queue.
if (!MessageQueue.Exists(".\\Private$\\newPrivTransQ2"))
{
MessageQueue.Create(".\\Private$\\newPrivTransQ2", true);
}
return;
}
}
}
```
以下是重构后的代码:
```csharp
Imports System.Messaging
Public Class MyNewQueue
' Provides an entry point into the application.
' This example creates new transactional queues.
Public Shared Sub Main()
' Create a new instance of the class.
Dim myNewQueue As New MyNewQueue
' Create transactional queues.
myNewQueue.CreatePublicTransactionalQueues()
myNewQueue.CreatePrivateTransactionalQueues()
Return
End Sub
' Creates public transactional queues and sends a message.
Public Sub CreatePublicTransactionalQueues()
' Create and connect to a public Message Queuing queue.
If Not MessageQueue.Exists(".\newPublicTransQueue1") Then
' Create the queue if it does not exist.
MessageQueue.Create(".\newPublicTransQueue1", True)
End If
' Connect to the queue.
Dim myNewPublicQueue As New MessageQueue(".\newPublicTransQueue1")
' Create a transaction.
Dim myTransaction As New MessageQueueTransaction
' Begin the transaction.
myTransaction.Begin()
' Send the message.
myNewPublicQueue.Send("My Message Data.", myTransaction)
' Commit the transaction.
myTransaction.Commit()
' Create (but do not connect to) a second queue.
If Not MessageQueue.Exists(".\newPublicTransQueue2") Then
MessageQueue.Create(".\newPublicTransQueue2", True)
End If
Return
End Sub
' Creates private queues and sends a message.
Public Sub CreatePrivateTransactionalQueues()
' Create and connect to a private Message Queuing queue.
If Not MessageQueue.Exists(".\Private$\newPrivTransQ1") Then
' Create the queue if it does not exist.
MessageQueue.Create(".\Private$\newPrivTransQ1", True)
End If
' Connect to the queue.
Dim myNewPrivateQueue As New MessageQueue(".\Private$\newPrivTransQ1")
' Create a transaction.
Dim myTransaction As New MessageQueueTransaction
' Begin the transaction.
myTransaction.Begin()
' Send the message.
myNewPrivateQueue.Send("My Message Data.", myTransaction)
' Commit the transaction.
myTransaction.Commit()
' Create (but do not connect to) a second private queue.
If Not MessageQueue.Exists(".\Private$\newPrivTransQ2") Then
MessageQueue.Create(".\Private$\newPrivTransQ2", True)
End If
Return
End Sub
End Class
```
```
注解:transactional=false若要在应用程序中创建类的新实例 MessageQueue 并将其绑定到现有队列,请使用 MessageQueue 构造函数。若要在消息队列中创建新队列,请调用 Create(String)。
path:MachineNameQueueName\MachineNamePrivate$QueueName
对本地计算机使用“.”。有关更多语法,请参阅 Path 属性。
下表显示了此方法在各种工作组模式下是否可用。
| 工作组模式 | 可用性 |
| -------- | ------ |
| 本地计算机 | 是 |
| 本地计算机和直接格式名称 | 是 |
| 远程计算机 | 否 |
| 远程计算机和直接格式名称 | 否 |
另请参阅:适用于
```