如何保存我的wlan设置?

时间:2017-09-29 14:30:10

标签: vb.net winforms visual-studio-2012 wlan

我目前正在使用Visual Studio 2012.我的表单代码在下面给出了设计截图。我正在尝试为我的PC创建一个虚拟热点,但我想添加一个以前的SSID和密码的功能。我的热点每次都以空白文本打开。

这是我的表单设计:

Form1 Design:

[Form 1 Design click to view]

Form1代码:

Imports System.Runtime.InteropServices
Imports System.Collections.ObjectModel
Imports System.Text
Imports NativeWifi

Public Class Form1
Public Const WM_NCLBUTTONDOWN As Integer = &HA1
Public Const HT_CAPTION As Integer = &H2`


   <DllImportAttribute("user32.dll")> _
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
      ByVal Msg As Integer, ByVal wParam As Integer, _
      ByVal lParam As Integer) As Integer
    End Function

    <DllImportAttribute("user32.dll")> _
    Public Shared Function ReleaseCapture() As Boolean
    End Function`

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim wlan = New WlanClient()
        Dim connectedSsids As Collection(Of [String]) = New Collection(Of String)()

        For Each wlanInterface As WlanClient.WlanInterface In wlan.Interfaces
            Dim ssid As Wlan.Dot11Ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid
            connectedSsids.Add(New [String](Encoding.ASCII.GetChars(ssid.SSID, 0, CInt(ssid.SSIDLength))))

            For Each item As String In connectedSsids
                Label1.Text = item
            Next
        Next
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.MouseEventArgs) _
      Handles Me.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            ReleaseCapture()
            SendMessage(Handle, WM_NCLBUTTONDOWN, _
               HT_CAPTION, 0)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If Form2.TextBox1.Text = "" Then
                MsgBox("Hotspot name cant be empty", MsgBoxStyle.Critical)
            End If
            If Form2.TextBox2.TextLength < 8 Then
                MsgBox("Password should be 8+ character", MsgBoxStyle.Critical)
                If Form2.TextBox2.Text = "" Then
                    MsgBox("Password cant be empty", MsgBoxStyle.Critical)
                End If
            Else
                Dim process As New Process()
                process.StartInfo.Verb = "runas"
                process.StartInfo.UseShellExecute = True
                process.Start("cmd", String.Format("/c {0} & {1} & {2}", "netsh wlan set hostednetwork mode=allow ssid=" & Form2.TextBox1.Text & " key=" & Form2.TextBox2.Text, "netsh wlan start hostednetwork", "pause"))
                Form2.CheckBox1.Enabled = False
                Form2.TextBox1.Enabled = False
                Form2.TextBox2.Enabled = False

                MsgBox("Hotspot started Successfully", MsgBoxStyle.Information)
            End If

        Catch
            MsgBox("Failed to Establish a hotspot", MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Process.Start("CMD", "/C netsh wlan stop hostednetwork")
        Form2.CheckBox1.Enabled = True
        Form2.TextBox1.Enabled = True
        Form2.TextBox2.Enabled = True
        MsgBox("Hotspot stopped Successfully", MsgBoxStyle.Information)
    End Sub

    Private Sub Clsbtn_Click(sender As Object, e As EventArgs) Handles Clsbtn.Click
        Me.Close()
    End Sub

    Private Sub Minbtn_Click(sender As Object, e As EventArgs) Handles Minbtn.Click
        Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
    End Sub

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Hide()
        Form2.Show()
    End Sub
End Class

Form2设计:

[Form2 Design Click to view]

0 个答案:

没有答案