在c#中获取所有连接的电脑的IP地址
这是我的代码。我只给出我的电脑的IP地址,但我想要在局域网中连接的所有计算机的IP地址。我也给出此代码中的故障排除错误。请给我更改代码的解决方案。
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();
}
Possible Duplicate:
get ip address of all pc on my pc which is connected in lan in c#
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全是专业人士,但我认为您需要的是端口扫描器,您可以将端口扫描器的基本功能实现到您的应用程序中,并读取网络中的所有实时 ip,
一个解决方案是:
http://www.geekpedia.com/tutorial142_Creating-a-Port -Scanner-with-Csharp.html
他们甚至在网站上提供可供下载的代码。还没有尝试过下载它很难。 :)
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. :)