显示LAN中的所有IP地址

时间:2013-03-06 10:11:35

标签: c# .net

我必须在ListBox中显示LAN的所有IP地址。当我试图绑定它的空。

//代码

        Process netUtility = new Process(); 
        netUtility.StartInfo.FileName = "net.exe";

        netUtility.StartInfo.CreateNoWindow = true;


        netUtility.StartInfo.RedirectStandardOutput = true;

        netUtility.StartInfo.UseShellExecute = false;

        netUtility.StartInfo.RedirectStandardError = true;

        netUtility.Start();



        StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream);



        string line = "";

        while ((line = streamReader.ReadLine()) != null)
        {

            if (line.StartsWith("\\"))
            {

                ListBox1.Items.Add(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper());

            }

        }

        streamReader.Close();
        netUtility.WaitForExit(1000); 

我哪里错了?

2 个答案:

答案 0 :(得分:1)

您可以简单地使用此方法,更灵活,更易于使用/理解:

C#代码: 来自此链接:Get All IP Addresses on Machine

    // Get host name
String strHostName = Dns.GetHostName();

// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

// Enumerate IP addresses
int nIP = 0;
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
    ....
}

答案 1 :(得分:0)

需要在流程中添加一行。

//代码

  netUtility.StartInfo.Arguments = "view";

现在它工作正常!!!

相关问题