C#判斷本地系統的網路連接狀態

來源:互聯網
上載者:User

函數InternetGetConnectedState返回本地系統的網路連接狀態。

文法:

BOOL InternetGetConnectedState(__out LPDWORD lpdwFlags,__in DWORD dwReserved); 參數: lpdwFlags[out]指向一個變數,該變數接收串連描述內容。該參數在函數返回FLASE時仍可以返回一個有效標記。該參數可以為下列值的一個或多個。
含義
INTERNET_CONNECTION_CONFIGURED0x40 Local system has a valid connection to the Internet, but it might or might not be currently connected.
INTERNET_CONNECTION_LAN 0x02 Local system uses a local area network to connect to the Internet.
INTERNET_CONNECTION_MODEM0x01 Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM_BUSY0x08 No longer used.
INTERNET_CONNECTION_OFFLINE 0x20 Local system is in offline mode.
INTERNET_CONNECTION_PROXY0x04 Local system uses a proxy server to connect to the Internet.
INTERNET_RAS_INSTALLED0x10 Local system has RAS installed.
dwReserved[in]保留值。必須為0。 傳回值:當存在一個modem或一個LAN串連時,返回TRUE,當不存在internet串連或所有的串連當前未被啟用時,返回false。當該函數返回false時,程式可以調用 GetLastError來接收錯誤碼。 樣本:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Runtime.InteropServices;namespace ConsoleApplication1{    class Program    {        private const int INTERNET_CONNECTION_MODEM = 1;        private const int INTERNET_CONNECTION_LAN = 2;        private const int INTERNET_CONNECTION_PROXY = 4;        private const int INTERNET_CONNECTION_MODEM_BUSY = 8;        [DllImport("winInet.dll ")]        //聲明外部的函數:        private static extern bool InternetGetConnectedState(            ref  int Flag,            int dwReserved        );        static void Main(string[] args)        {            int Flag = 0;            string netStates = "";            if (!InternetGetConnectedState(ref Flag, 0))            {                Console.WriteLine("no!");            }            else            {                if ((Flag & INTERNET_CONNECTION_MODEM) != 0)                    netStates += " Connect by MODEM /n";                if ((Flag & INTERNET_CONNECTION_LAN) != 0)                    netStates += "Connect by LAN  /n";                if ((Flag & INTERNET_CONNECTION_PROXY) != 0)                    netStates += "Connect by PROXY /n";                if ((Flag & INTERNET_CONNECTION_MODEM_BUSY) != 0)                    netStates += " MODEM is busy  /n";            }            Console.WriteLine(netStates);            Console.ReadLine();        }    }}

 


 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.