2013-11-26 71 views
0

我對編程一般都很陌生,所以答案可能非常簡單。我試圖將每個NIC的所有IP顯示在標籤中。我可以在控制檯中運行相同的代碼,並使其正確輸出。我可以將它添加到列表框並正確顯示,但我無法弄清楚如何獲得更多的IP到標籤中。希望這是有道理的。我在互聯網上發現了大部分代碼,並試圖瞭解它們如何一起工作,所以如果您看到一些可能無意義的註釋行,那麼您知道爲什麼。有問題的標籤是label8.Text如何通過C#中的標籤顯示多個IP地址

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net.NetworkInformation; 
using System.Net; 
using System.Net.Sockets; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      //first we should create an array of the network interfaces 
      NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 

      //now lets iterate through the list of nics and call them adapter 
      foreach (NetworkInterface adapter in nics) 
      { 
       //add the adapter description to the listbox1 items 
       listBox1.Items.Add(adapter.Description); 
      } 
     } 

     public void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      //Lets make our form taller so that the labels can be seen. 
      Form1.ActiveForm.Height = 320; 

      //Get the currently chosen listBox item. 
      string nicname = listBox1.Text; 

      //Again build an array of all the Network Interfaces. 
      NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 

      //Lets loop through those interfaces 
      foreach (NetworkInterface adapter in nics) 
      { 
       //Lets condition on the adapter description making sure it equals the nicname of the chosen adapter in the listbox1 
       if (nicname == adapter.Description) 
       { 
        //Lets update all the labels to display more information about the chosen adapter. 
        label1.Text = adapter.Description; 
        label2.Text = "Name: " + adapter.Name; 
        label3.Text = "Type: " + adapter.NetworkInterfaceType; 
        label4.Text = "Status: " + adapter.OperationalStatus; 
        label5.Text = "Speed: " + adapter.Speed; 
        label6.Text = "Multicast Support? " + adapter.SupportsMulticast; 
        label7.Text = "MAC: " + adapter.GetPhysicalAddress(); 

        //NetworkInterface[] prop = NetworkInterface.GetAllNetworkInterfaces(); 
        IPInterfaceProperties properties = adapter.GetIPProperties(); 

        foreach (IPAddressInformation uniCast in properties.UnicastAddresses) 
        { 
         label8.Text = "IP: " + (uniCast.Address); 
         //label11.Text = "IP: " + uniCast.Address; 
         //listBox2.Items.Add(uniCast.Address); 
         foreach (UnicastIPAddressInformation uipi in adapter.GetIPProperties().UnicastAddresses) 
         label10.Text = "Subnet: " + uipi.IPv4Mask; 
        } 
        foreach (GatewayIPAddressInformation GateWay in properties.GatewayAddresses) 
        { 
         label9.Text = "Gateway: " + GateWay.Address; 
        } 
       } 
      } 
     } 
    } 
} 

回答

1

每次都重新這裏的標籤:

label8.Text = "IP: " + (uniCast.Address); 

,你可以把它改成:

label8.Text += (uniCast.Address); 

label8.Text = label8.Text + (uniCast.Address); 

那麼你只需要把:

label8.Text = "IP: "; 

在循環之前把IP的前綴放在它上面。

懶惰版本:

if (nicname == adapter.Description) 
{ 
    //Lets update all the labels to display more information about the chosen adapter. 
    label1.Text = adapter.Description; 
    label2.Text = "Name: " + adapter.Name; 
    label3.Text = "Type: " + adapter.NetworkInterfaceType; 
    label4.Text = "Status: " + adapter.OperationalStatus; 
    label5.Text = "Speed: " + adapter.Speed; 
    label6.Text = "Multicast Support? " + adapter.SupportsMulticast; 
    label7.Text = "MAC: " + adapter.GetPhysicalAddress(); 
    label8.Text = "IP: "; 

    //NetworkInterface[] prop = NetworkInterface.GetAllNetworkInterfaces(); 
    IPInterfaceProperties properties = adapter.GetIPProperties(); 

    foreach (IPAddressInformation uniCast in properties.UnicastAddresses) 
    { 
     label8.Text += (uniCast.Address); 
     label10.Text = "Subnet: "; 

     foreach (UnicastIPAddressInformation uipi in adapter.GetIPProperties().UnicastAddresses) 
     label10.Text += uipi.IPv4Mask; 
    } 
    foreach (GatewayIPAddressInformation GateWay in properties.GatewayAddresses) 
    { 
     label9.Text = "Gateway: " + GateWay.Address; 
    } 
} 
1

那是因爲你這樣做在foreach:

label8.Text = "IP: " + (uniCast.Address); 

你重寫你的透水文本。

可以使用+=連接或使用字符串生成器進行構建,然後將其用於標籤。