呵呵,刚好我写的有一个,分全给我喽!/// <summary>
/// This Class is used read and write message from MessageQueue
/// </summary>
public class Cls_MessageMG
{
public static void clear_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
}
MessageQueue mq=new MessageQueue(Queue_Path);
mq.Purge();
} public static void send_message(string Queue_Path, string Msg_Content, Msg_Type Msg_Type_Instance)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
}
MessageQueue mq=new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
switch (Msg_Type_Instance)
{
case Msg_Type.XML:
mq.Formatter=new XmlMessageFormatter();
break;
case Msg_Type.Binary:
mq.Formatter=new BinaryMessageFormatter();
break;
case Msg_Type.ActiveX:
mq.Formatter=new ActiveXMessageFormatter();
break;
}
mq.Send(Msg_Content);
} public static string receive_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return "";
} MessageQueue mq= new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
((XmlMessageFormatter)mq.Formatter).TargetTypeNames=new string[]{"System.String,mscorlib"}; try
{
System.Messaging.Message m = mq.Receive();
return (string)m.Body;
}
catch (Exception ex)
{
return (ex.ToString());
}
} public static string peek_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return "";
} MessageQueue mq= new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
((XmlMessageFormatter)mq.Formatter).TargetTypeNames=new string[]{"System.String,mscorlib"}; try
{
System.Messaging.Message m = mq.Peek();
return (string)m.Body;
}
catch (Exception ex)
{
return (ex.ToString());
}
} public static bool IsQueueEmpty(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
bool isQueueEmpty = false; if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return true;
} // Connect to a queue.
MessageQueue myQueue = new MessageQueue(Queue_Path); try
{
// Set Peek to return immediately.
myQueue.Peek(new TimeSpan(0)); // If an IOTimeout was not thrown, there is a message 
// in the queue.
isQueueEmpty = false;
} catch(MessageQueueException e)
{
if (e.MessageQueueErrorCode == 
MessageQueueErrorCode.IOTimeout)
{
// No message was in the queue.
isQueueEmpty = true;
} // Handle other sources of MessageQueueException.
} // Handle other exceptions as necessary. // Return true if there are no messages in the queue.
return isQueueEmpty;
}
public static int GetQueueCount(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path; if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return 0;
} MessageQueue myQueue = new MessageQueue(Queue_Path);
MessageEnumerator myEnumerator = myQueue.GetMessageEnumerator(); int message_count=0;

while (myEnumerator.MoveNext())
{
message_count++;
}
return message_count;
}}

解决方案 »

  1.   

    参考--Using MSMQ from C#:
    http://www.codeproject.com/csharp/mgpMyQueue.aspReliable Messaging with MSMQ and .NET:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdadotnetasync2.asp
      

  2.   

    To顶楼:
    呵呵,谢谢你这么真诚,把你辛苦写的代码都共享了。
    不过,我是需要访问“传出队列”的方法,跟你的还
    不一样。
    你是不是对MQ比较熟悉呢?
    [email protected]
    有空可以联络我。