一个基于C#编写的基础网络信息检测工具

华盟原创文章投稿奖励计划

一个基于C#编写的基础网络信息检测工具


这个程序设计了五个模块,如下:
1.获取本地主机的主机名、ipv4地址、ipv6地址
2.获取远程主机的ip地址、延迟等
3.获取网络适配器信息(网卡信息),包含名称、mac地址等
4.获取网络流量信息,包含ipv4的统计信息:所在域、接收数据包、发送数据包、丢弃数据包等
5.调用cmd执行网络状态重置命令:netsh winsock reset

获取本地主机信息代码如下:

this.richTextBox1.Clear();

//获取主机名,回环
string localHostName = Dns.GetHostName();
IPAddress loopbackip = IPAddress.Loopback;

richTextBox1.AppendText(" 主机名:" localHostName "\n");
richTextBox1.AppendText(" 回环地址:" loopbackip);
//获取本机ipv4
IPHostEntry localhost = Dns.GetHostEntry(localHostName);
IPAddress[] iPAddressesList = Dns.GetHostAddresses(localHostName);

//输出ipv6
foreach (IPAddress ipa in iPAddressesList)
{
   if (ipa.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
   {
      richTextBox1.AppendText("\n IPv6:" ipa);
   }
}

//输出ipv4
foreach (IPAddress ipa in iPAddressesList)
{
   if (ipa.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
   {
      richTextBox1.AppendText("\n IPv4地址:" ipa);
   }
}
richTextBox1.AppendText("\n 可分配端口最小值:" IPEndPoint.MinPort);
richTextBox1.AppendText("\n 可分配端口最大值:" IPEndPoint.MaxPort "\n");
GetNetworkFlow();

获取远程主机信息代码如下:

this.richTextBox2.Clear();

IPHostEntry remotaHost = Dns.GetHostEntry(this.textBox1.Text);
IPAddress[] remotaIP = remotaHost.AddressList;
foreach (IPAddress ipa in remotaIP)
{
   if(ipa.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
   {
      richTextBox2.AppendText(" 目标主机地址:" ipa "\n");
   }
}
string hostAddress = this.textBox1.Text.Trim();
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string testData = "Test Data";
byte[] buffer = Encoding.ASCII.GetBytes(testData);
int timeout = 120;

//调用同步的Send方法发送数据,将结果保存至PingReply实例
PingReply reply = pingSender.Send(hostAddress, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
   richTextBox2.AppendText(" 往返时间:" reply.RoundtripTime "\n");
   richTextBox2.AppendText(" 生存时间:" reply.Options.Ttl "\n");
   richTextBox2.AppendText(" 是否控制数据包的分段:" reply.Options.DontFragment "\n");
   richTextBox2.AppendText(" 缓冲区大小:" reply.Buffer.Length "\n");
}

获取网络适配器信息代码如下:

NetworkInterface[] AdapterInfoBox = NetworkInterface.GetAllNetworkInterfaces();

for (int i = 0; i < AdapterInfoBox.Length; i ) 
{
   richTextBox3.AppendText("   ------------------------------------第" (i 1) "个网络适配器信息------------------------------------\n");
   richTextBox3.AppendText("   描述信息:" AdapterInfoBox.Description "\n");
   richTextBox3.AppendText("   名称:" AdapterInfoBox.Name "\n");
   richTextBox3.AppendText("   类型:" AdapterInfoBox.NetworkInterfaceType "\n");
   richTextBox3.AppendText("   速度:" AdapterInfoBox.Speed / 1000 / 1000 "\n");
   richTextBox3.AppendText("   MAC地址:" BitConverter.ToString(AdapterInfoBox.GetPhysicalAddress().GetAddressBytes()) "\n");

获取网络流量代码如下:

//获取包含本机的网络连接和通信统计数据的信息的对象
IPGlobalProperties NetworkInfo = IPGlobalProperties.GetIPGlobalProperties();
//获取本机IPv4统计数据
IPGlobalStatistics NetworkFlow = NetworkInfo.GetIPv4GlobalStatistics();

richTextBox1.AppendText("\n 网络流量统计:\n");
richTextBox1.AppendText(" 本机所在域  ······  " NetworkInfo.DomainName "\n");
richTextBox1.AppendText(" 接收数据包  ······  " NetworkFlow.ReceivedPackets "\n");
richTextBox1.AppendText(" 传送数据包  ······  " NetworkFlow.ReceivedPacketsDelivered "\n");
richTextBox1.AppendText(" 转发数据包  ······  " NetworkFlow.ReceivedPacketsForwarded "\n");
richTextBox1.AppendText(" 丢弃数据包  ······  " NetworkFlow.ReceivedPacketsDiscarded "\n");

调用cmd执行命令的代码如下:

string reset = "netsh winsock reset";
string restart = "shutdown -r -t 0";
//创建实例
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();

//向cmd窗口发送输入信息
p.StandardInput.WriteLine(reset "&" restart "&exit");
p.StandardInput.AutoFlush = true;

this.Close();

本文原创,作者:张,其版权均为华盟网所有。如需转载,请注明出处:https://www.77169.net/html/265181.html

发表评论