在运行进程的网络上创建计算机

时间:2014-03-08 15:16:11

标签: c# networking process

我正在尝试在运行特定进程的本地网络上查找计算机。为此,我有以下功能:

private IPAddress GetLocalIP()
    {
        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
                return ip;
        }
        return IPAddress.Parse("127.0.0.1");
    }

    private string GetFriendIP()
    {

        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip != localIP)
            {
                Console.WriteLine(GetMachineNameFromIPAddress(ip.ToString()));
                Process[] allProcs = Process.GetProcesses(GetMachineNameFromIPAddress(ip.ToString()));
                Console.WriteLine(GetMachineNameFromIPAddress(ip.ToString()));

                foreach (Process p in allProcs)
                {
                    if (p.ProcessName == "You'll Catch A virus")
                        return GetMachineNameFromIPAddress(ip.ToString()) + ": " + ip.ToString();
                }
            }


        }
        return "No PCs connected on network";
    }

此代码显示Couldn't connect to remote machine处的错误Process.GetProcesses(GetMachineNameFromIPAddress(ip.ToString()))。虽然如果我将条件更改为ip == localIP,则错误消失,并且函数返回没有找到任何机器。这有什么问题?

1 个答案:

答案 0 :(得分:0)

根据the msdn,当目标pc运行XP,NT或2000时会触发此InvalidOperationException。是否适合您?在这种情况下,您不能使用声明不支持的此方法。

否则,您可能需要阅读SecurityDemands上的文档,尽管相关错误似乎没有触发您收到的错误消息。