Back to Top

Daily Archives: 2017年9月15日

VB.NET WiFi接続プロファイルを削除する

無線LANの接続のプロファイルを削除する方法を記載します。

以下説明は、”Managed Wifi”を使用して行います。

プロファイルの削除

Imports NativeWifi


Dim targetInterface = "Wi-Fi"               'ワイヤレス ネットワーク接続";
Dim targetProfile = "XXXXXXXXXXXXX-2G"

Dim client As WlanClient = New WlanClient()

'インタフェースを取得する
Dim wlanIface = (From x In client.Interfaces() Where x.InterfaceName = targetInterface Select x).FirstOrDefault()
If wlanIface Is Nothing Then
    Return
End If

'プロファイルが存在するかチェックする
Dim profile = (From x In wlanIface.GetProfiles() Where x.profileName = targetProfile Select x.profileName).FirstOrDefault()
If profile Is Nothing Then
    Return
End If

'インタフェースの接続状態をチェック
If wlanIface.InterfaceState = Wlan.WlanInterfaceState.Connected Then
    'プロファイル名が接続中かチェック
    If wlanIface.CurrentConnection.profileName = targetProfile Then
        Return
    End If
End If

'プロファイルを削除する
wlanIface.DeleteProfile(targetProfile)

※プロファイルを削除する場合は、無線LAN接続を切断してから削除してください。

※プログラムの例は、同一PCに、同じレシーバーや同じルーターが無いものとして作成しています。

 

VB.NET プロファイルにあるWiFi接続

既に登録されているプロファイル中の無線LANに接続する方法を記載します。

以下説明は、”Managed Wifi”を使用して行います。

プロファイル中の無線LANに接続をする

Imports NativeWifi

Dim targetInterface = "Wi-Fi"            'ワイヤレス ネットワーク接続";
Dim targetProfile = "XXXXXXXXXXXXX-2G"

Dim client As WlanClient = New WlanClient()

'先頭のインタフェースを取得する
Dim wlanIface = client.Interfaces().Where(Function(x) x.InterfaceName = targetInterface).FirstOrDefault()
If wlanIface Is Nothing Then
    Return
End If

'プロファイルが存在するかチェックする
Dim profile = (From x In wlanIface.GetProfiles() Where x.profileName = targetProfile Select x.profileName).FirstOrDefault()
If profile Is Nothing Then
    Return
End If

'インタフェースの接続状態をチェック
If wlanIface.InterfaceState = Wlan.WlanInterfaceState.Connected Then
    '接続中のプロファイル名が接続したいプロファイル名と一致するかチェック
    If wlanIface.CurrentConnection.profileName = targetProfile Then
        Return
    End If
End If

'接続を開始する
wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, targetProfile)