代码如下: using System; using System.Management; namespace TestWMI { class Program { static void Main(string[] args) { CreateRP("My RP"); CreateRL("My RL", "net.msmq://localhost/test1", "My RP"); CreateSP("My SP", "net.msmq://localhost/test2"); } // Create Receive Port static void CreateRP(string receivePortName) { try { PutOptions options = new PutOptions(); options.Type = PutType.CreateOnly; ManagementClass objClass = new ManagementClass("root//MicrosoftBizTalkServer", "MSBTS_ReceivePort", null); ManagementObject objRP = objClass.CreateInstance(); objRP["Name"] = receivePortName; objRP["IsTwoWay"] = "FALSE"; objRP.Put(options); } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); } } // Create Receive Location static void CreateRL(string receiveLocationName, string address, string receivePortName) { try { PutOptions options = new PutOptions(); options.Type = PutType.CreateOnly; ManagementClass objClass = new ManagementClass("root//MicrosoftBizTalkServer", "MSBTS_ReceiveLocation", null); ManagementObject objRL = objClass.CreateInstance(); objRL["Name"] = receiveLocationName; objRL["ReceivePortName"] = receivePortName; objRL["AdapterName"] = "WCF-NetMsmq"; objRL["HostName"] = "BizTalkServerApplication"; objRL["PipelineName"] = "Microsoft.BizTalk.DefaultPipelines.PassThruReceive, Microsoft.BizTalk.DefaultPipelines, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; objRL["InboundTransportURL"] = address; objRL.Put(options); } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); } } // Create Send Port static void CreateSP(string sendPortName, string address) { try { PutOptions options = new PutOptions(); options.Type = PutType.CreateOnly; ManagementClass objClass = new ManagementClass("root//MicrosoftBizTalkServer", "MSBTS_SendPort", null); ManagementObject objSP = objClass.CreateInstance(); objSP["Name"] = sendPortName; objSP["IsDynamic"] = "FALSE"; objSP["IsTwoWay"] = "FALSE"; objSP["PTTransportType"] = "WCF-NetMsmq"; objSP["SendPipeline"] = "Microsoft.BizTalk.DefaultPipelines.PassThruTransmit, Microsoft.BizTalk.DefaultPipelines, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; objSP["PTAddress"] = address; objSP.Put(options); } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); } } } }