在c#中获取所有连接的电脑的IP地址

发布于 2024-10-23 15:55:52 字数 1319 浏览 3 评论 0原文

可能的重复:
在c#中获取我的电脑上连接到局域网的所有电脑的IP地址

这是我的代码。我只给出我的电脑的IP地址,但我想要在局域网中连接的所有计算机的IP地址。我也给出此代码中的故障排除错误。请给我更改代码的解决方案。

static void Main(string[] args)
{
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "cmd ";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.Arguments = "/C net view";
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    String output = p.StandardOutput.ReadToEnd();
    char[] delimiters = new char[] { '\n' };
    String strHostName = string.Empty;

    string[] s = output.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
    IPAddress[] addr = ipEntry.AddressList;



    p.WaitForExit();
    int z = s.Length - 5;


    string[] str1 = new string[z];
    int i = 0;
    char[] saperator = { ' '};
    for (int j = 3; j < s.Length - 2; j++, i++)
    {

        str1[i] = (s[j].ToString()).Split(saperator)[0] ;
        Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
    }
     //Console.WriteLine(output);

    Console.ReadLine();
}

Possible Duplicate:
get ip address of all pc on my pc which is connected in lan in c#

This is my code.I give only ip address of my computer but i want to ip address of all computer which is connected in lan.also i give the troubleshoot error in this code. please give me the solution with changes of code.

static void Main(string[] args)
{
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "cmd ";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.Arguments = "/C net view";
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    String output = p.StandardOutput.ReadToEnd();
    char[] delimiters = new char[] { '\n' };
    String strHostName = string.Empty;

    string[] s = output.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
    IPAddress[] addr = ipEntry.AddressList;



    p.WaitForExit();
    int z = s.Length - 5;


    string[] str1 = new string[z];
    int i = 0;
    char[] saperator = { ' '};
    for (int j = 3; j < s.Length - 2; j++, i++)
    {

        str1[i] = (s[j].ToString()).Split(saperator)[0] ;
        Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
    }
     //Console.WriteLine(output);

    Console.ReadLine();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦浮鱼 2024-10-30 15:55:52

我不完全是专业人士,但我认为您需要的是端口扫描器,您可以将端口扫描器的基本功能实现到您的应用程序中,并读取网络中的所有实时 ip,

一个解决方案是:
http://www.geekpedia.com/tutorial142_Creating-a-Port -Scanner-with-Csharp.html

他们甚至在网站上提供可供下载的代码。还没有尝试过下载它很难。 :)

Im not exactly a professional but i think what you need is called a port scanner, you can implement the very basic functionality of a port scanner into your app and read all the live ips in your network

one solution would be:
http://www.geekpedia.com/tutorial142_Creating-a-Port-Scanner-with-Csharp.html

they even have code available for download on the site. haven't tried downloading it tough. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文