一般来说,我对编程很陌生,所以答案可能很简单。我试着把来自每个网卡的所有IP显示到一个标签中。我可以在控制台中运行相同的代码,并使其正确地串出。我可以将它添加到一个列表框中,并正确地显示它,但我不知道如何将多个IP添加到标签中。希望这是合理的。我在互联网上找到了这段代码的大部分,并试图了解这些代码是如何一起工作的,所以如果你看到一些可能没有意义的注释行,你就知道为什么了。正在讨论的标签是8.文本
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;
}
}
}
}
}
}发布于 2013-11-26 00:34:59
每次您在这里重置标签时:
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;
}
}发布于 2013-11-26 00:35:55
那是因为你这么做是在前头:
label8.Text = "IP: " + (uniCast.Address); 你在重写你的过激短信。
要么使用+=连接,要么使用字符串生成器构建它,然后将其用于标签。
https://stackoverflow.com/questions/20206287
复制相似问题