专业的编程技术博客社区

网站首页 > 博客文章 正文

C#刷新WiFi列表,获取wifi列表(c#读取网页)

baijin 2024-09-08 01:49:11 博客文章 15 ℃ 0 评论

在C#中刷新Wi-Fi列表通常意味着重新枚举系统上可用的无线网络。这可以通过调用Windows的本地API来实现,或者通过P/Invoke调用Windows的网络管理API。以下是一个使用Windows API来刷新Wi-Fi列表的示例代码:

首先,你需要添加对wlanapi.dll的引用,这个DLL包含了管理无线网络的函数。

csharpusing System;
using System.Runtime.InteropServices;
using System.Text;

class Program
{
    // 导入wlanapi.dll中的函数
    [DllImport("wlanapi.dll")]
    public static extern int WlanOpenHandle(out IntPtr phClientHandle, ref object pReserved);

    [DllImport("wlanapi.dll")]
    public static extern int WlanEnumInterfaces(IntPtr hClientHandle, out IntPtr ppInterfaceList);

    [DllImport("wlanapi.dll")]
    public static extern int WlanCloseHandle(IntPtr hClientHandle, IntPtr hInterfaceHandle);

    [DllImport("wlanapi.dll")]
    public static extern int WlanGetAvailableNetworkList(IntPtr hInterfaceHandle, ref uint pdwFlags, IntPtr pReserved, out IntPtr ppAvailableNetworkList);

    [DllImport("wlanapi.dll")]
    public static extern int WlanFreeMemory(IntPtr pMemory);

    static void Main()
    {
        IntPtr clientHandle = IntPtr.Zero;
        IntPtr interfaceList = IntPtr.Zero;
        IntPtr availableNetworkList = IntPtr.Zero;
        uint flags = 0;

        try
        {
            // 打开客户端句柄
            if (WlanOpenHandle(out clientHandle, null) != 0)
            {
                Console.WriteLine("Failed to open WLAN client handle.");
                return;
            }

            // 枚举所有网络接口
            if (WlanEnumInterfaces(clientHandle, out interfaceList) != 0)
            {
                Console.WriteLine("Failed to enumerate WLAN interfaces.");
                return;
            }

            // 假设我们只关心第一个接口
            IntPtr firstInterface = Marshal.ReadIntPtr(interfaceList);

            // 获取可用网络列表
            if (WlanGetAvailableNetworkList(firstInterface, ref flags, IntPtr.Zero, out availableNetworkList) != 0)
            {
                Console.WriteLine("Failed to get available network list.");
                return;
            }

            // 这里你可以处理availableNetworkList,它包含了所有可用的网络
            // 注意:这里只是演示如何获取列表,并没有实际处理网络数据

            // 释放内存
            WlanFreeMemory(availableNetworkList);
        }
        finally
        {
            // 关闭接口句柄和客户端句柄
            if (interfaceList != IntPtr.Zero)
            {
                WlanFreeMemory(interfaceList);
            }

            if (clientHandle != IntPtr.Zero)
            {
                WlanCloseHandle(clientHandle, IntPtr.Zero);
            }
        }
    }
}

在这个示例中,我们使用了WlanOpenHandle来打开一个客户端句柄,WlanEnumInterfaces来枚举所有网络接口,WlanGetAvailableNetworkList来获取第一个接口的可用网络列表。最后,我们使用WlanFreeMemory来释放分配的内存,并使用WlanCloseHandle来关闭句柄。

请注意,这个示例仅适用于Windows操作系统,并且可能需要在管理员权限下运行才能正常工作。此外,还需要处理错误和异常,以及适当地释放资源。

如果你想要一个更高级别的抽象,你可以考虑使用Windows的NetworkListManager类,这是.NET Framework的一部分,它提供了管理网络连接的更简单的API。但是,请注意,这个类可能不会在所有的Windows版本上都可用。

在C#中,获取Wi-Fi列表通常涉及调用Windows的本地API(如Wlanapi.dll)或使用Windows的PowerShell命令,然后通过C#代码执行这些命令。从Windows 10开始,你还可以使用Windows.Devices.WiFi命名空间中的UWP(Universal Windows Platform)API来获取Wi-Fi列表,但这要求你的应用程序是UWP应用。

以下是几种获取Wi-Fi列表的方法:

使用Wlanapi.dll

这是通过P/Invoke调用Windows本地API的方法。

csharpusing System;
using System.Runtime.InteropServices;
using System.Text;

class Program
{
    [DllImport("wlanapi.dll")]
    public static extern int WlanEnumInterfaces(out IntPtr ppInterfaceList, out int pdwCount);

    [DllImport("wlanapi.dll")]
    public static extern int WlanGetAvailableNetworkList(IntPtr hInterface, out IntPtr ppAvailableNetworkList);

    [DllImport("wlanapi.dll")]
    public static extern int WlanFreeMemory(IntPtr pMemory);

    static void Main()
    {
        IntPtr pInterfaces = IntPtr.Zero;
        IntPtr pAvailableNetworks = IntPtr.Zero;
        int count = 0;

        try
        {
            // 枚举所有WLAN接口
            if (WlanEnumInterfaces(out pInterfaces, out count) != 0)
            {
                Console.WriteLine("Failed to enumerate WLAN interfaces.");
                return;
            }

            // 假设我们只有一个接口
            IntPtr firstInterface = Marshal.ReadIntPtr(pInterfaces);

            // 获取第一个接口的可用网络列表
            if (WlanGetAvailableNetworkList(firstInterface, out pAvailableNetworks) != 0)
            {
                Console.WriteLine("Failed to get available network list.");
                return;
            }

            // 处理可用网络列表...
            // 注意:这里只是示例代码,并没有实际处理网络数据

            // 释放内存
            WlanFreeMemory(pAvailableNetworks);
        }
        finally
        {
            // 释放接口列表内存
            if (pInterfaces != IntPtr.Zero)
            {
                WlanFreeMemory(pInterfaces);
            }
        }
    }
}

使用PowerShell命令

你可以通过C#的System.Management.Automation命名空间执行PowerShell命令来获取Wi-Fi列表。

csharpusing System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

class Program
{
    static void Main()
    {
        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
            runspace.Open();

            using (PowerShell powerShell = PowerShell.Create())
            {
                powerShell.Runspace = runspace;
                powerShell.AddCommand("Get-NetworkAvailableSsid");
                powerShell.AddParameter("InterfaceDescription", "Wireless Network Connection"); // 可能需要根据你的网络接口名称进行调整

                Collection<PSObject> psOutput = powerShell.Invoke();

                foreach (PSObject outputItem in psOutput)
                {
                    Console.WriteLine(outputItem.Properties["SSID"].Value.ToString());
                }
            }

            runspace.Close();
        }
    }
}

使用UWP API (仅限UWP应用)

如果你的应用是UWP应用,你可以使用Windows.Devices.WiFi命名空间中的API。

csharpusing System;
using Windows.Devices.WiFi;
using Windows.Foundation;
using Windows.Networking.Connectivity;

class Program
{
    static async void Main()
    {
        // 检查设备是否支持WiFi
        if (!WiFiAdapter.AdapterExists)
        {
            Console.WriteLine("WiFi adapter not found.");
            return;
        }

        // 获取默认的WiFi适配器
        WiFiAdapter adapter = await WiFiAdapter.FindAdapterAsync();

        // 获取网络报告
        NetworkReport networkReport = await adapter.GetNetworkReportAsync();

        // 遍历所有可用的网络
        foreach (WiFiAvailableNetwork network in networkReport.AvailableNetworks)
        {
            Console.WriteLine(#34;SSID: {network.Ssid}");
            Console.WriteLine(#34;Signal bar strength: {network.SignalBarStrength}");
            Console.WriteLine(#34;Authentication type: {network.AuthenticationType}");
            // ...其他属性
        }
    }
}

请注意,UWP API只能在UWP应用中使用,不能在传统的Windows桌面应用

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表