Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用WMI在C#中启动/停止BizTalk发送端口_C#_Wmi_Biztalk - Fatal编程技术网

使用WMI在C#中启动/停止BizTalk发送端口

使用WMI在C#中启动/停止BizTalk发送端口,c#,wmi,biztalk,C#,Wmi,Biztalk,是否可以通过C使用MSBTS_SendPort类启动/停止发送端口 我已使用Windows WMI获取端口详细信息和状态,并尝试启动/停止端口 public bool CheckSendPorts() { bool returnValue = true; UserName = ""; Password = ""; ServerName = "testserver"

是否可以通过C使用MSBTS_SendPort类启动/停止发送端口

我已使用Windows WMI获取端口详细信息和状态,并尝试启动/停止端口

  public bool CheckSendPorts()
    {
        bool returnValue = true;
        UserName = "";
        Password = "";
        ServerName = "testserver";
        using (ManagementObjectSearcher searcher = GetWmiSearcher(UserName,Password,ServerName, WMI_SCOPE, $"SELECT * FROM MSBTS_SendPort where Name = 'testSendPort'"))
        {
            if (searcher == null)
            {
                //WriteOutput($"No Send Ports found.", true);
                return false;
            }

            foreach (ManagementObject instanceObject in searcher.Get())
            {
                string portName = instanceObject["Name"] as string;
                uint portState = (uint)instanceObject["Status"];
                string portStatus = GetPortStatus((uint)instanceObject["Status"]);
                bool ignoreLocation = false;

                
            }
        }

        return returnValue;
    }

    internal ManagementObjectSearcher GetWmiSearcher(string username,string password,string servername, string wmiScope, string wmiQuery)
    {

        ConnectionOptions connectionOptions = new ConnectionOptions();

        connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;

        if (!string.IsNullOrEmpty(username))
        {
            connectionOptions.Username = username;
            connectionOptions.Password = password;
        }
        else
        {
            connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
        }

        ManagementScope scope = new ManagementScope($@"\\{servername}{wmiScope}");
        scope.Options = connectionOptions;

        EnumerationOptions enumOptions = new EnumerationOptions();
        enumOptions.ReturnImmediately = false;

        SelectQuery query = new SelectQuery(wmiQuery);

        return new ManagementObjectSearcher(scope, query, enumOptions);
    }

  private string GetPortStatus(uint code)
    {
        switch (code)
        {
            case 1:
                return "Bound";

            case 2:
                return "Stopped";

            case 3:
                return "Started";

            default:
                return "Unknown";
        }
    }
Microsoft文档建议有一种方法作为
MSBTS\u SendPort.Stop
来停止端口。有可能吗?

在迭代实例时,可以在foreach循环中调用“stop”或“start”方法

        foreach (ManagementObject instanceObject in searcher.Get())
        {
            string portName = instanceObject["Name"] as string;
            uint portState = (uint)instanceObject["Status"];
            string portStatus = GetPortStatus((uint)instanceObject["Status"]);
            bool ignoreLocation = false;

            //invoke method stop with an empty object array for parameters (because this method doesn't take any parameters.
            instanceObject.InvokeMethod("stop", new object[]{ });
         
        }

当您尝试该方法时会发生什么情况?@Dijkgraaf我无法使用它,因为它需要microsoft.biztalk.explorerom,但我的系统上没有安装biztalk