C# 檢查是否聯網

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace LocalApp.ConsoleApp.Core
{
    
public class Net
    
{
        [DllImport(
"wininet")]
        
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        
/**//// <summary>
        
/// 檢測本機是否聯網
        
/// </summary>
        
/// <returns></returns>

        public static bool IsConnectedInternet()
        
{
            
int i = 0;
            
if (InternetGetConnectedState(out i, 0))
            
{
                
//已聯網
                return true;
            }

            
else
            
{
                
//未聯網
                return false;
            }


        }


    }

}

發佈了6 篇原創文章 · 獲贊 9 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章