Hi!

I am trying to create an application in C# that will take a list of IPs from a text file and display whether or not a ping is returned from each IP.
I was wondering if it was possible, with a loop or something similar, to create two labels for each IP, one for the name and one for the status, dynamically, every time the program is run.

How would I go about doing this?

Thanks!

Dani AI

Generated

— dynamic labels work (as and showed), but for a list of IPs a row-based control is easier to manage, update and scale. Use a ListView (Details) or a DataGridView with two columns (IP, Status), populate rows with a "Checking..." placeholder, then run asynchronous pings and update the status cells. That keeps the UI responsive and avoids manual layout math for every run.

A concise pattern (WinForms, async/await, limit concurrency):

private async Task CheckIpsAsync(string[] ips)
{
    foreach (var ip in ips)
        listView1.Items.Add(new ListViewItem(new[] { ip, "Checking..." }));

    var sem = new SemaphoreSlim(50); // tune concurrency
    var tasks = ips.Select(async ip =>
    {
        await sem.WaitAsync();
        try
        {
            using var ping = new Ping();
            var reply = await ping.SendPingAsync(ip, 1000);
            var status = reply.Status == IPStatus.Success ? $"{reply.RoundtripTime} ms" : reply.Status.ToString();
            UpdateRow(ip, status);
        }
        catch (Exception ex) { UpdateRow(ip, "Error: " + ex.Message); }
        finally { sem.Release(); }
    }).ToArray();

    await Task.WhenAll(tasks);
}

private void UpdateRow(string ip, string status)
{
    if (listView1.InvokeRequired) listView1.BeginInvoke(new Action(() => UpdateRow(ip, status)));
    else
    {
        var it = listView1.Items.Cast<ListViewItem>().FirstOrDefault(i => i.Text == ip);
        if (it != null) it.SubItems[1].Text = status;
    }
}

Practical notes: ICMP can be blocked by firewalls — a failed ping doesn't always mean "down." If you need service-level reachability, try a TCP connect to a known port (TcpClient.ConnectAsync). Always dispose Ping objects, set a sensible timeout, catch exceptions, and provide a CancellationToken for user-abort. For very large lists consider virtual mode (ListView/DataGridView) or paging; if you still want labels, put them inside a FlowLayoutPanel to avoid hard-coded position math.

Recommended Answers

All 3 Replies

Read through the text file line by line in a loop.
And for each iteration, declare the two labels and add them to the forms Control collection.

int counter = 0;
string line;
int lastKnownTop = 10; //Or whereever you want the first set of labels to be placed

System.IO.StreamReader file = new System.IO.StreamReader("path");
while ((line = file.ReadLine()) != null)
{
    Label ip = new Label();
    ip.Position(new Point(10, lastKnownTop));
    ip.Text = line;

    Label status = new Label();
    status.Position(new Point(25, lastKnownTop));
    status.Text = CheckStatus(ip).ToString();

    form1.Controls.Add(ip);
    form1.Controls.Add(status);

    lastKnownTop = (ip.Top + ip.Height) + 5;
}
file.Close();

You can do in a loop:

int x = 20, y = 20; //initial location x and y
for(int i = 0; i < 2; i++)
{
    Label l = new Label();
    l.Position = new Point(x, y);
    l.Text = i == 0 ?" Text for label 1" : "Text for label 2";
    this.Controls.Add(l);
    //set new location for next:
    x+= 50; //2nd will be 50pixels to the right
}

Oh! I see.

So the form has a collection of controls you can just add to? That's good to know!

Thanks! I will attempt it later!

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.