【问题标题】:C#: Why I can only connect to my ip address but the other ip address, getting error (WMI)C#:为什么我只能连接到我的 IP 地址,但另一个 IP 地址,得到错误(WMI)
【发布时间】:2018-04-05 09:49:52
【问题描述】:

我正在尝试使用我的电脑连接到其他网络,但出现错误

(System.Runtime.InteropServices.COMException: 'RPC 服务器不可用。(来自 HRESULT 的异常: 0x800706BA)')

但是,它只有在我连接到我的电脑时才有效。除了这个过程之外还有什么方法吗,因为如果它像防火墙、我的代码或我的防病毒软件等,我什至无法弄清楚是什么错误。

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());
    }
}

【问题讨论】:

    标签: c# networking


    【解决方案1】:

    我认为你应该仔细阅读这篇文章

    Connecting to WMI Remotely with C#

    1. 使用计算机名称和 WMI 路径创建一个ManagementScope 对象,并通过调用ManagementScope.Connect() 连接到您的目标。

    2. 如果您连接到不同域中的远程计算机或使用不同的用户名和密码,则必须在调用ManagementScope 时使用ConnectionOptions 对象。

    ConnectionOptions 包含用于描述 身份验证、模拟、用户名、密码和其他 连接选项。

    示例

    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);
    ...
    

    一般来说,除非明确需要,否则建议您将模拟级别设置为模拟。

    假设您拥有正确的凭据,我认为这将是防火墙问题,或远程计算机上的 Windows 设置。

    我将首先应用以下防火墙规则并向后工作。或者为了测试完全禁用目标机器上的防火墙

    • COM+ 网络访问 (DCOM-In)

    • 远程事件日志管理(NP-In)

    • 远程事件日志管理 (RPC)

    • 远程事件日志管理 (RPC-EPMAP)

    • Windows 管理规范(异步输入)

    • Windows 管理规范 (DCOM-In)

    • Windows 管理规范 (WMI-In)


    补充阅读

    Connecting to WMI Remotely with C#

    ManagementScope Class

    ConnectionOptions Class

    ObjectGetOptions Class

    ManagementPath Class

    ManagementClass Class

    免责声明:您必须阅读这些主题并确定您的情况需要什么。

    【讨论】:

    • @DevGeorge 这似乎不太可能。我的意思是这些 api 就是为此而设计的
    • 先生。可以问一下连接打开吗??向字符串声明了什么样的用户名和密码值?我不明白 ConnectionOptions Options = new ConnectionOptions(); //字符串域=“我的域”;字符串域 = ""; //Options.Username = "管理员"; //Options.Password = "我的密码";
    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2014-06-07
    • 2014-09-08
    相关资源
    最近更新 更多