我正在创建一个应用程序,并且对于其中一个功能,我需要获取我网络的IP地址。
ping 192.168.1.255 & arp-a
将列出我需要的每个IP地址,格式为表格:
Internet Address Physical Address Type
192.168.x.x xx-xx-xx-xx-xx-xx dynamic
192.168.x.x xx-xx-xx-xx-xx-xx dynamic
代码就是这样的:
private void button_Click(object sender, EventArgs e)
{
string strCmdText3;
strCmdText3 = "/c ping 192.168.1.255 & arp -a";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = strCmdText3;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
//Console.WriteLine(output);
//string err = process.StandardError.ReadToEnd();
//Console.WriteLine(err);
process.WaitForExit();
show.Text = output;
}
现在我不知道的是我怎么才能获得IP地址(将它们放入数组或其他东西),所以我可以在以后使用它们来执行不同的命令。
这将如何
"\r\nPinging 192.168.1.255 with 32 bytes of data:\r\nRequest timed out.\r\nRequest timed out.\r\nRequest timed out.\r\nRequest timed out.\r\n\r\nPing statistics for 192.168.1.255:\r\n Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),\r\n\r\nInterface: 10.10.10.74 --- 0x7\r\n Internet Address Physical Address Type\r\n 10.10.80.1 10-da-43-72-3a-99 dynamic \r\n 10.10.80.335 00-e0-4c-c7-44-0c dynamic \r\n 10.10.80.290 e0-ac-cb-60-88-c4 dynamic \r\n 10.10.80.25 34-02-86-a0-79-72 dynamic \r\n 10.10.80.27 00-1c-c0-8a-59-e7 dynamic \r\n
答案 0 :(得分:0)
PING
命令,您可以重新添加,但稍后会跳过更多行。在大多数情况下,几乎没有需要,这是更新的代码
static void Main() {
string strCmdText3;
strCmdText3 = "/c arp -a";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = strCmdText3;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
//Console.WriteLine(output);
//string err = process.StandardError.ReadToEnd();
//Console.WriteLine(err);
process.WaitForExit();
//show.Text = output;
string[] LinesReturned = output.Split( '\n' );
List<string> ipAddresses = new List<string>();
for (int i = 3; i < LinesReturned .GetUpperBound(0); i++) {
ipAddresses.Add(LinesReturned [i].Substring(2, 15).Trim());
}