本文介绍了获得在C#中所有连接的电脑的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
This is my code.I give only ip address of my computer but i want to ip address of all computer which is connected in lan.also i give the troubleshoot error in this code. please give me the solution with changes of code.
static void Main(string[] args)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/C net view";
p.StartInfo.RedirectStandardOutput = true;
p.Start();
String output = p.StandardOutput.ReadToEnd();
char[] delimiters = new char[] { '\n' };
String strHostName = string.Empty;
string[] s = output.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
p.WaitForExit();
int z = s.Length - 5;
string[] str1 = new string[z];
int i = 0;
char[] saperator = { ' '};
for (int j = 3; j < s.Length - 2; j++, i++)
{
str1[i] = (s[j].ToString()).Split(saperator)[0] ;
Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
}
//Console.WriteLine(output);
Console.ReadLine();
}
解决方案
Im not exactly a professional but i think what you need is called a port scanner, you can implement the very basic functionality of a port scanner into your app and read all the live ips in your network
one solution would be:http://www.geekpedia.com/tutorial142_Creating-a-Port-Scanner-with-Csharp.html
they even have code available for download on the site. haven't tried downloading it tough. :)
这篇关于获得在C#中所有连接的电脑的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!