C#中是否有简单的代码来检查PC是否已连接到互联网? [英] is there a simple code in C# to check if pc is connected to internet?
本文介绍了C#中是否有简单的代码来检查PC是否已连接到互联网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
至少,该代码还可以识别它是否已连接和不再连接,请提供帮助,因为我希望我将其放入我的项目中,但我真的不知道该怎么做.在此先感谢=)
atleast the code could also identify if it is connected and not connected anymore pleasee help because i want i to put into my project i have really no idea how to do it. thanks in advance =)
推荐答案
如何: ^ ]
最好的问候
Espen Harlinn
How about:Get the Available Network Connections Using WMI[^]
Best regards
Espen Harlinn
使用 InternetGetConnectedState [^ ] 功能.然后使用 InternetCheckConnection [
Use InternetGetConnectedState[^] function. And then use InternetCheckConnection[^] function. A sample code:
using System;
using System.Runtime;
using System.Runtime.InteropServices;
public class InternetCS
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );
public static bool IsConnectedToInternet( )
{
int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}
}
-OR-
-OR-
WebClient web = new WebClient();
try
{
web.DownloadData("http://www.codeproject.com");
}
catch (WebException ex)
{
Console.WriteLine("Could not connect");
}
如果失败,则没有连接.但是我认为第一种方法更可靠.
祝你好运,
OI
If it fails you do not have conenction. But 1 st way is more reliable in my opinion.
Good luck,
OI
检查此提示/技巧
测试Internet连接 [
Check this Tip/Trick
Testing Internet Connectivity[^](Contains 5 different ways)
这篇关于C#中是否有简单的代码来检查PC是否已连接到互联网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文