简体   繁体   English

C#:为什么我只能连接到我的IP地址,而另一个IP地址却出现错误(WMI)

[英]C#: Why I can only connect to my ip address but the other ip address, getting error (WMI)

I'm trying to connect to other network using my pc but im getting error 我正在尝试使用我的PC连接到其他网络,但我收到了错误消息

(System.Runtime.InteropServices.COMException: 'The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)') (System.Runtime.InteropServices.COMException:'RPC服务器不可用。(HRESULT的异常:0x800706BA)')

However, It's only working if I connect to my pc itself. 但是,仅当我连接到PC本身时,它才有效。 Is there any approach aside from this process because I cant even figure what's the error if it's like firewall,my codes or my anti-virus etc. 除了此过程外,还有什么方法可以解决,因为我什至无法弄清楚如果发生了什么错误,例如防火墙,我的代码或我的防病毒软件等。

private void Form2_Load(object sender, EventArgs e)
{

    // remote computer.
    ManagementScope scope =
        new ManagementScope(
        "\\\\172.20.1.50\\root\\cimv2");
    scope.Connect();


    //Query system for Operating System information
    ObjectQuery query = new ObjectQuery(
        "SELECT * FROM Win32_OperatingSystem");
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher(scope, query);

    ManagementObjectCollection queryCollection = searcher.Get();
    foreach (ManagementObject m in queryCollection)
    {
        // Display the remote computer information


        MessageBox.Show(m["csname"].ToString());
    }
}

I think you should read through this 我认为你应该通读

Connecting to WMI Remotely with C# 使用C#远程连接到WMI

  1. Create a ManagementScope object, using the name of the computer and the WMI path, and connect to your target with a call to ManagementScope.Connect() . 使用计算机的名称和WMI路径创建一个ManagementScope对象,并通过调用ManagementScope.Connect()连接到您的目标。

  2. If you connect to a remote computer in a different domain or using a different user name and password, then you must use a ConnectionOptions object in the call to the ManagementScope . 如果连接到其他域中的远程计算机或使用其他用户名和密码,则必须在对ManagementScope的调用中使用ConnectionOptions对象。

The ConnectionOptions contains properties for describing the Authentication, Impersonation, username, password, and other connection options. ConnectionOptions包含用于描述身份验证,模拟,用户名,密码和其他连接选项的属性。

Exmaple 枫树

ConnectionOptions options = new ConnectionOptions();
options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

// options takes more arguments, you need to read up on what you want

ManagementScope scope = new ManagementScope("\\\\FullComputerName\\root\\cimv2", options);
scope.Connect();

ManagementPath path = new ManagementPath("Win32_NetworkAdapterConfiguration");
ObjectGetOptions o = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
ManagementClass objMC = new ManagementClass(scope, path, o);
...

Generally speaking, it is recommended that you set your Impersonation level to Impersonate unless explicitly needed otherwise 一般来说,除非明确需要,否则建议您将模拟级别设置为“模拟”

Assuming you have the correct credentials, i think this will turn out to be a firewall issue, or a windows setting on the remote computer. 假设您具有正确的凭据,我认为这将是防火墙问题或远程计算机上的Windows设置。

Id apply the following firewall rules first and work backwards. 请先应用以下防火墙规则,然后再反向进行。 Or for a test completely disable firewalls on the target machine 或进行测试以完全禁用目标计算机上的防火墙

  • COM+ Network Access (DCOM-In) COM +网络访问(DCOM输入)

  • Remote Event Log Management (NP-In) 远程事件日志管理(NP-In)

  • Remote Event Log Management (RPC) 远程事件日志管理(RPC)

  • Remote Event Log Management (RPC-EPMAP) 远程事件日志管理(RPC-EPMAP)

  • Windows Management Instrumentation (ASync-In) Windows管理规范(ASync-In)

  • Windows Management Instrumentation (DCOM-In) Windows管理规范(DCOM-In)

  • Windows Management Instrumentation (WMI-In) Windows管理规范(WMI-In)


Additional reading 补充阅读

Connecting to WMI Remotely with C# 使用C#远程连接到WMI

ManagementScope Class ManagementScope类别

ConnectionOptions Class ConnectionOptions类别

ObjectGetOptions Class ObjectGetOptions类别

ManagementPath Class ManagementPath类别

ManagementClass Class 管理类

Disclaimer : You will have to read about these topics and work out what you need in your situation. 免责声明 :您将必须阅读有关这些主题的信息,并弄清自己所处的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM