程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 用C#使用API即時判斷當前的網絡的連接方式

用C#使用API即時判斷當前的網絡的連接方式

編輯:C#入門知識

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

namespace MeshworkLinkMode
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("使用InternetGetConnectedState對網絡連接方式進行判斷");
            Console.WriteLine(Fun_InternetGetConnectedState());
            Console.WriteLine("使用IsNetworkAlive對網絡連接方式進行判斷");
            Console.WriteLine(Fun_IsNetworkAlive());
            Console.ReadLine();
        }

        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        [DllImport("sensapi.dll")]
        private extern static bool IsNetworkAlive(out int connectionDescription);

        static string Fun_InternetGetConnectedState()
        {
            int INTERNET_CONNECTION_MODEM = 1;
            int INTERNET_CONNECTION_LAN = 2;
            int INTERNET_CONNECTION_PROXY = 4;
            int INTERNET_CONNECTION_MODEM_BUSY = 8;

            string outPut = null;
            int flags;//上網方式
            bool m_bOnline = true;//是否在線 

            m_bOnline = InternetGetConnectedState(out flags, 0);
            if (m_bOnline)//在線  
            {
                if ((flags & INTERNET_CONNECTION_MODEM) == INTERNET_CONNECTION_MODEM)
                {
                    outPut = "在線:撥號上網 ";
                }
                if ((flags & INTERNET_CONNECTION_LAN) == INTERNET_CONNECTION_LAN)
                {
                    outPut = "在線:通過局域網 ";
                }
                if ((flags & INTERNET_CONNECTION_PROXY) == INTERNET_CONNECTION_PROXY)
                {
                    outPut = "在線:代理 ";
                }
                if ((flags & INTERNET_CONNECTION_MODEM_BUSY) == INTERNET_CONNECTION_MODEM_BUSY)
                {
                    outPut = "MODEM被其他非INTERNET連接占用 ";
                }
            }
            else
            {
                outPut = "不在線 ";
            }

            return outPut;
        }

        static string Fun_IsNetworkAlive()
        {
            int NETWORK_ALIVE_LAN = 0;
            int NETWORK_ALIVE_WAN = 2;
            int NETWORK_ALIVE_AOL = 4;

            string outPut = null;
            int flags;//上網方式
            bool m_bOnline = true;//是否在線 

            m_bOnline = IsNetworkAlive(out flags);
            if (m_bOnline)//在線  
            {
                if ((flags & NETWORK_ALIVE_LAN) == NETWORK_ALIVE_LAN)
                {
                    outPut = "在線:NETWORK_ALIVE_LAN ";
                }
                if ((flags & NETWORK_ALIVE_WAN) == NETWORK_ALIVE_WAN)
                {
                    outPut = "在線:NETWORK_ALIVE_WAN ";
                }
                if ((flags & NETWORK_ALIVE_AOL) == NETWORK_ALIVE_AOL)
                {
                    outPut = "在線:NETWORK_ALIVE_AOL ";
&nb

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved