怎么样判断网络通不通啊?
解决方案 »
- 二元决策图程序求助
- excel下载提示时 如果是直接打开 文件名是乱码
- 高手求救,asp.net 调用dll的问题
- winform的listBox
- SocketAsyncEventArgs文件传输
- 请问在一个包含水晶报表的Web页面中,如何实现直接打印?
- 用DataGrid连接数据库,要建一个类,而这个类到底有什么功能?好模糊
- 请问:如何将文本框内的文本转换为HTML格式字符串,我是要用于页面上的格式化输出的,最好提供代码
- 请问这是为什么?是BUG吗?
- 关于c#,out方法的问题,求帮忙
- C#如何从com口读取相关数据,请问各位大侠是怎么做的
- 求肋:winform中给用户控件添加属性(就像font属性一样,可以在属性窗口显示)
public static bool IsAnyNetworkAvailable()
{
try
{
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.OperationalStatus == OperationalStatus.Up &&
ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
{
IPInterfaceProperties iip = ni.GetIPProperties();
if (iip != null && iip.UnicastAddresses != null && iip.UnicastAddresses.Count > 0)
{
return true;
}
}
}
return false;
}
catch
{
//如果有异常,直接调用API判断
return IsInternetConnected();
}
} [DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(out int connectionDescription, int reservedValue); public static bool IsInternetConnected()
{
int i;
return InternetGetConnectedState(out i, 0);
}
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
System.Net.NetworkInformation.NetworkInterface[] _NetFace = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (System.Net.NetworkInformation.NetworkInterface _OneNet in _NetFace)
{
MessageBox.Show(_OneNet.OperationalStatus.ToString());
} }
2能不能访问外面 你直接socket连接外面的地址就好了
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
System.Net.NetworkInformation.NetworkInterface[] _NetFace = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (System.Net.NetworkInformation.NetworkInterface _OneNet in _NetFace)
{
MessageBox.Show(_OneNet.OperationalStatus.ToString());
} }
2能不能访问外面 你直接socket连接外面的地址就好了
using System;using System.Diagnostics;namespace ZZ{ class ZZConsole { [STAThread] static void Main(string[] args) { string ip = "192.192.132.229"; string strRst = CmdPing(ip); Console.WriteLine(strRst); Console.ReadLine(); } private static string CmdPing(string strIp) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string pingrst; p.Start(); p.StandardInput.WriteLine("ping -n 1 "+strIp); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd(); if(strRst.IndexOf("(0% loss)")!=-1) pingrst = "连接"; else if( strRst.IndexOf("Destination host unreachable.")!=-1) pingrst = "无法到达目的主机"; else if(strRst.IndexOf("Request timed out.")!=-1) pingrst = "超时"; else if(strRst.IndexOf("Unknown host")!=-1) pingrst = "无法解析主机"; else pingrst = strRst; p.Close(); return pingrst; } }}
请问这句话要写在哪啊?怎么提示我说: 找不到类型或命名空间名称“DllImport”(是否缺少 using 指令或程序集引用?)
using System.Runtime.InteropServices;就当函数来写啊
你这还不如用CMD ping 啊
namespace internet
{ public class Class1
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int connectionDescription, int reservedValue ) ;
public Class1(){}
private bool IsConnected()
{
int I=0;
bool state = InternetGetConnectedState(out I,0);
return state;
}
} }