c#判断是否联网
2013-02-26 11:37
232 查看
方法一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
namespace 判断是否联网
{
class Program
{
static void Main(string[] args)
{
Ping p = new Ping();
PingReply pr;
pr = p.Send("119.75.218.45");//百度的IP
if (pr.Status != IPStatus.Success)//如果连接不成功
{
Console.WriteLine("未联网");
}
else
{
Console.WriteLine("已联网");
}
Console.Read();
}
}
}
c#关于判断网络连接正常与否的总结
本人最近做c#winform的项目,遇到了判断网络是否正常连接的问题。后来查出了以下几种方法,供大家学习参考。
1.方法二(用异步调用)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Demo : Form
{
public Demo()
{
InitializeComponent();
}
//判断
private void btpanduan_Click(object sender, EventArgs e)
{
//210.192.120.228 163网易
string ip = this.txtip.Text.ToString();
int port = Convert .ToInt32( this.txtport.Text.ToString());
bool a = panduan(ip, port );//135为本机服务端口号
if (a == true)
{
MessageBox.Show("该网络连接正常 !");
}
else
{
MessageBox.Show("该网络连接不畅通 !");
}
}
// 异步调用
//判断的方法
public bool panduan(string ip, int port)
{
try
{
TcpClient client = new TcpClient(ip, port);
if (client.Connected)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
}
2.利用 c# ping类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Demo3 : Form
{
public Demo3()
{
InitializeComponent();
}
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply res;
//检查网络连接
private void btcheck_Click(object sender, EventArgs e)
{
string url = this.txturl.Text.ToString();
bool a = check(url);
if (a == true)
{
MessageBox.Show("连接成功!", "提示信息");
}
else
{
MessageBox.Show("连接失败!", "提示信息");
}
}
public bool check(string url)
{
try
{
res = ping.Send(url);
if (res.Status == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
catch {
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
namespace 判断是否联网
{
class Program
{
static void Main(string[] args)
{
Ping p = new Ping();
PingReply pr;
pr = p.Send("119.75.218.45");//百度的IP
if (pr.Status != IPStatus.Success)//如果连接不成功
{
Console.WriteLine("未联网");
}
else
{
Console.WriteLine("已联网");
}
Console.Read();
}
}
}
c#关于判断网络连接正常与否的总结
本人最近做c#winform的项目,遇到了判断网络是否正常连接的问题。后来查出了以下几种方法,供大家学习参考。
1.方法二(用异步调用)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Demo : Form
{
public Demo()
{
InitializeComponent();
}
//判断
private void btpanduan_Click(object sender, EventArgs e)
{
//210.192.120.228 163网易
string ip = this.txtip.Text.ToString();
int port = Convert .ToInt32( this.txtport.Text.ToString());
bool a = panduan(ip, port );//135为本机服务端口号
if (a == true)
{
MessageBox.Show("该网络连接正常 !");
}
else
{
MessageBox.Show("该网络连接不畅通 !");
}
}
// 异步调用
//判断的方法
public bool panduan(string ip, int port)
{
try
{
TcpClient client = new TcpClient(ip, port);
if (client.Connected)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
}
2.利用 c# ping类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Demo3 : Form
{
public Demo3()
{
InitializeComponent();
}
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply res;
//检查网络连接
private void btcheck_Click(object sender, EventArgs e)
{
string url = this.txturl.Text.ToString();
bool a = check(url);
if (a == true)
{
MessageBox.Show("连接成功!", "提示信息");
}
else
{
MessageBox.Show("连接失败!", "提示信息");
}
}
public bool check(string url)
{
try
{
res = ping.Send(url);
if (res.Status == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
catch {
return false;
}
}
}
}
相关文章推荐
- C# ADO.net判断是否可以联网
- 整理笔记,发现判断是否联网的C#代码 --..--
- 【分享】C#如何快速判断系统是否已经联网?
- C#实现判断电脑是否联网
- C#技巧:判断系统是否已经联网
- c# 判断是否联网
- C# 判断系统系统是否联网
- C#判断电脑是否处于联网状态
- [C#]判断计算机是否处于连网(联网)状态
- C#_判断本机是否联网
- C# 判断是否联网
- c# 注册表操作,创建,删除,修改,判断节点是否存在
- C#控制台基础 判断dictionary中是否已经存在一个键
- [C#]方法示例:判断是否闰年
- C#中判断字符串是否由数字组成
- C#中判断某软件是否已安装
- handler线程,判断是否联网,Get获取网页 json解析,输入流转换字符串
- C# object 判断值是否相同
- c#判断字符串是否为汉字
- C# 判断是否为数字的几种方法