基本信息
源码名称:测试网ip地址ping 结果
源码大小:0.42M
文件格式:.rar
开发语言:C#
更新时间:2016-11-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
c# 测试网络是否稳定 通过ping ip又有异常就会记录下来
c# 测试网络是否稳定 通过ping ip又有异常就会记录下来
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
//using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
namespace GeiIPState
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string M_TbxStatus = "";
string GetIP = "";
string path = Application.StartupPath "\\IP_Status_Log.txt";
public void tbx_getIP_Leave()
{
try
{
int _int = GetIP.IndexOf("w");
if (_int >= 0 && GetIP.Substring(_int, 3) == "www")
{
if (IsUrl(GetIP.Trim()))
{
this.lab_prompt.Text = "";
return;
}
else
{
this.lab_prompt.Text = "格式有误!";
return;
}
}
else
{
if (IPCheck(GetIP.Trim()))
{
this.lab_prompt.Text = "";
return;
}
else
this.lab_prompt.Text = "格式有误!";
}
}
catch (Exception)
{
this.lab_prompt.Text = "格式有误!";
return;
throw;
}
}
public bool IPCheck(string IP)
{
try
{
string regformat = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
return regex.IsMatch(IP);
}
catch (Exception)
{
this.lab_prompt.Text = "格式有误!";
throw;
}
}
public bool IsUrl(string str_url)
{
try
{
return System.Text.RegularExpressions.Regex.IsMatch(str_url, @"(?<![\w@] )((http|https)://)?(www.)?[a-z0-9\.] (\.(com|net|cn|com\.cn|com\.net|net\.cn))(/[^\s\n]*)?");
}
catch (Exception)
{
this.lab_prompt.Text = "格式有误!";
throw;
}
}
public void Ping(string ip)
{
try
{
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply reply = p.Send(ip);
String Show_IP_States = "";
if (reply.Status.ToString() == "Success")
{
if (is_ip_status)
{
Show_IP_States = GetIP ": 时间【" reply.RoundtripTime "ms】 TTL【" reply.Options.Ttl "】";
Clear_Itme(Show_IP_States);
}
return;
}
else
if (reply.Status.ToString() == "DestinationHostUnreachable")
{
M_TbxStatus = "[" DateTime.Now "] :" GetIP " 【无法访问目标主机】";
Show_IP_States= GetIP ": 【无法访问目标主机】";
Writer_Log(M_TbxStatus);
if (is_ip_status)
{
Show_IP_States = GetIP ": 【无法访问目标主机】";
Clear_Itme(Show_IP_States);
}
}
else
{
M_TbxStatus = "[" DateTime.Now "] :" GetIP " 【请求超时】";
Writer_Log(M_TbxStatus);
if (is_ip_status)
{
Show_IP_States = GetIP ": 【请求超时】";
Clear_Itme(Show_IP_States);
}
}
}
catch (Exception)
{
this.lab_prompt.Text = "格式有误!";
return;
throw;
}
}
public void Clear_Itme(String Show_IP_States)
{
if (lbx_IPStatus.Items.Count == 50)
lbx_IPStatus.Items.Clear();
lbx_IPStatus.Items.Add(Show_IP_States);
}
private void btn_start_Click(object sender, EventArgs e)
{
{
GetIP = this.tbx_getIP.Text.ToLower();
if ( timer1.Enabled == false && GetIP.Trim()!="")
{
tbx_getIP_Leave();
if (this.lab_prompt.Text == "")
{
btn_start.Text = "停止";
timer1.Enabled = true;
timer1.Start();
}
}
else if ( timer1.Enabled == true)
{
if (timer1.Enabled == true)
{
btn_start.Text = "开始";
timer1.Enabled = false;
}
}
}
}
public void Writer_Log(String _log)
{
try
{
StreamWriter SW;
if (!File.Exists(path))
{
SW = File.CreateText(path);
SW.Close();
}
else
{
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(_log);
sw.Close();
fs.Close();
}
}
catch (Exception)
{
return;
throw;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
if (timer1.Enabled == true)
Ping(GetIP.Trim());
backgroundWorker1.RunWorkerAsync();
}
catch (Exception)
{
return;
throw;
}
}
private void tbn_stop_Click(object sender, EventArgs e)
{
StreamWriter SW;
if (File.Exists(path))
{
System.Diagnostics.Process.Start(path);
}
else
{
SW = File.CreateText(path);
SW.Close();
}
}
bool is_ip_status = true;
private void ckbn_IPStatus_CheckedChanged(object sender, EventArgs e)
{
if (ckbn_IPStatus.Checked == true)
is_ip_status = true;
else
is_ip_status = false;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}
}