//获取IP地址 public string GetIPAddress() { IPAddress ip = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address); return ip.ToString(); }
判断网络已经连接[DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue); public bool IsInternetConnected() { int i = 0; return InternetGetConnectedState(out i, 0); }
AutoResetEvent waiter = new AutoResetEvent(false); Ping myPing = new Ping(); myPing.PingCompleted += new PingCompletedEventHandler(myPing_PingCompleted); string data = "OK"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 10000; PingOptions options = new PingOptions(64, true); myPing.SendAsync(_AppState.ServerName, timeout, buffer, options, waiter); 或 IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); TcpConnectionInformation[] connections = properties.GetActiveTcpConnections(); foreach (TcpConnectionInformation info in connections) { if (info.RemoteEndPoint.Equals(targetEndPoint)) { _AppState.OnlineStatus = Constant.ONLINE_STATUS_ONLINE; isOnline = true; break; } } 或 [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue); System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; for (int i = 0; i < addressList.Length; i ++) { s += addressList[i].ToString(); }
string st = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { System.Array ar; ar = (System.Array)(mo.Properties["IpAddress"].Value); st = ar.GetValue(0).ToString(); break; } } moc = null; mc = null;
给一段最简单的代码,自己创建一个Winform工程,然后放两个TEXTBOX,一个Buttion 思路很简单,能够读取网页就代表能上网了.既然能读取网页,那么就跑去某个IP查证的网页去读取自己的IP不就OK了? Imports System.Net Imports System.Text.RegularExpressions Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim wb As WebClient = New WebClient Dim strContent As String = "" Dim strIP As String = "" Dim regIP As Regex = New Regex("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}") Try strContent = wb.DownloadString("http://www.ip138.com/ips8.asp") If strContent.IndexOf("IP地址查询") >= 1 Then TextBox1.Text = "爽,可以上网!" strIP = regIP.Match(strContent).ToString If strIP.Trim <> "" Then TextBox2.Text = "俺的IP是:" & strIP Else TextBox2.Text = "虽然俺上网了,但是俺是黑户,没IP!" End If End If Catch ex As Exception TextBox1.Text = "真不爽,不能上网,我去炸了电信!" TextBox2.Text = "不能上网,没IP!" Finally If Not wb Is Nothing Then wb.Dispose() End Try End Sub End Class
你尝试连一个公共的网站,连通就可以说明网络是已经连接的了。
public string GetIPAddress()
{
IPAddress ip = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
return ip.ToString();
}
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue); public bool IsInternetConnected()
{
int i = 0; return InternetGetConnectedState(out i, 0);
}
直接连接路由器得到的IP地址应该是192.168.*.*
直接MODEM,如果已拨号连接,得到的IP地址正确
如果未拨号连接得到的IP地址应该是本机地址127.0.0.16楼判断连接也不行,我使用路由器,即使路由器不连接MODEM也是显示连接状态
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(myPing_PingCompleted);
string data = "OK";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 10000;
PingOptions options = new PingOptions(64, true);
myPing.SendAsync(_AppState.ServerName, timeout, buffer, options, waiter);
或
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation info in connections) {
if (info.RemoteEndPoint.Equals(targetEndPoint)) {
_AppState.OnlineStatus = Constant.ONLINE_STATUS_ONLINE;
isOnline = true;
break;
}
}
或
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i ++)
{
s += addressList[i].ToString();
}
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
System.Array ar;
ar = (System.Array)(mo.Properties["IpAddress"].Value);
st = ar.GetValue(0).ToString();
break;
}
}
moc = null;
mc = null;
思路很简单,能够读取网页就代表能上网了.既然能读取网页,那么就跑去某个IP查证的网页去读取自己的IP不就OK了?
Imports System.Net
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wb As WebClient = New WebClient
Dim strContent As String = ""
Dim strIP As String = ""
Dim regIP As Regex = New Regex("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")
Try
strContent = wb.DownloadString("http://www.ip138.com/ips8.asp")
If strContent.IndexOf("IP地址查询") >= 1 Then
TextBox1.Text = "爽,可以上网!"
strIP = regIP.Match(strContent).ToString
If strIP.Trim <> "" Then
TextBox2.Text = "俺的IP是:" & strIP
Else
TextBox2.Text = "虽然俺上网了,但是俺是黑户,没IP!"
End If
End If
Catch ex As Exception
TextBox1.Text = "真不爽,不能上网,我去炸了电信!"
TextBox2.Text = "不能上网,没IP!"
Finally
If Not wb Is Nothing Then wb.Dispose()
End Try
End Sub
End Class
如果你使用的不是Windows,不好意思这段代码不能用... ...
如果你说你上网设置都没设好... ... 压根就不能上网,但是你的路由确实已经连入Internet,不好意思该段也不能用..
哈哈哈
但是我没看懂,能不能写详细点给我看,我是新手,昨天才使用C#
比如要添加什么using也顺便说下咯
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
WebClient wb = new WebClient();
String strIP = "";
String strContent = "";
Regex regIP = new Regex("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
try
{
strContent = wb.DownloadString("http://www.ip138.com/ips8.asp");
if (strContent.IndexOf("IP地址查询") >= 1)
{
textBox1.Text = "爽,可以上网!";
strIP = regIP.Match(strContent).ToString();
if (strIP.Trim() != "")
{
textBox2.Text = "俺的IP是:" + strIP;
}
else
{
textBox2.Text = "虽然俺上网了,但是俺是黑户,没IP!";
}
}
}
catch
{
textBox1.Text = "真不爽,不能上网,我去炸了电信!";
textBox2.Text = "不能上网,没IP!";
}
}
}
}
有没有不依赖别人网站的程序检测网络状态和IP地址?
所以你不可能直接从你本机就得知你是否Internet已经连通,必须发起请求给路由,路由再检测是否已经连通Internet,然后返回是否成功的标记以及返回请求的内容.
所以不论什么程式一定不会只是检测本地的链接,需要跟路由交互,路由跟ISP DNS打交道.
而且其实存在是否真正连通的问题
即使路由已经取得外网IP,也不见得就是通的,因为电信有时候出现假链接的情况
所以我的方法是最笨也是最保险的.
你想下, google 每天挂掉的机率是 1/10W (至少我最近几年顶多听到几次google中国瘫痪的)
baidu 也是差不多的几率
你觉得不保险,你可以 同时用我的代码去访问 几个门户或者搜索引擎,同时挂的几率貌似接近于0吧?
退一步说,如果同时挂了,是不是就代表电信线路挂了,你也压根就不能上网呢?
Debug.WriteLine(True)
Else
Debug.WriteLine(False)
End If另外可以参考C#检测网络的连接状态
{
System.Net.IPAddress addr;
// 获得本机局域网IP地址
addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
return addr.ToString ( ) ;
}
}
Ping ping =new Ping();
PingReply res;
res=ping.Send("www.baidu.com");
if (res.Status != IPStatus.Success)
{
"没有联网"
}
这样子不更好吗?自己在完善一下就OK了