MessageQueue myQueue = new MessageQueue(".\private$\myQueue"); try { Message myMessage = new Message(); myMessage.Body = "消息内容"; myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); //发送消息到队列中 myQueue.Send(myMessage); } catch (ArgumentException e) { Console.WriteLine(e.Message); } Message myMessage2 = new Message(); myMessage2 = myQueue.Receive(); myMessage2.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) }); string context = (string)myMessage2.Body; //获取消息的内容 Console.WriteLine("消息内容为:" + context); Console.ReadKey();