显示所有来自局域网的IP地址
本文关键字:IP 地址 局域网 显示 | 更新日期: 2023-09-27 18:02:25
我必须在列表框中显示我的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);
我错在哪里?
在这里您可以简单地使用这种方法,这种方法更加灵活和易于使用/理解:
c#代码:获取机器上的所有IP地址
// 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)
{
....
}
需要在Process中添加一行
//代码 netUtility.StartInfo.Arguments = "view";
现在可以正常工作了!!