user1512559
user1512559

Reputation:

Display all the IP Addresses from LAN

I have to display all the IP Address of my LAN in a ListBox. When i'm trying to bind its empty.

// Code

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

Where i'm wrong?

Upvotes: 0

Views: 2340

Answers (2)

Mehdi Bugnard
Mehdi Bugnard

Reputation: 3979

Where you can simply use this method much more flexible and easy to use / understand :

C# code: from this link : 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)
{
    ....
}

Upvotes: 1

user1512559
user1512559

Reputation:

Need to add a line in the Process.

// Code

  netUtility.StartInfo.Arguments = "view";

Now it works fine!!!

Upvotes: 0

Related Questions