I. Introduction
Wireless settings can be exported and imported using netsh. In this article, I will refer to the wlan(wireless local area network) part of the netsh
command line program for the actions explained. Besides importing and exporting profiles and their wireless information, other actions can be performed
such as: deleting wireless profiles (also called networks), blocking certain networks from being showing up in the wireless connections list, etc.
One example is netsh wlan delete will delete a wireless network. Next, let’s see the form setup so everyone reading the article can understand how
the project was designed before I step into the code.
II. Setup Of Project
For this project I have 2 forms in the setup of the project. For importing, I have
Form1 which imports the data using an open file dialog box which can accept
multiple items. For exporting profiles, the Profiles form exports specific profiles the
user checks from the checked list box. Figure 1 is my design of Form1 and Figure 2
shows the design of the Profiles form. The profiles form is used to display all the
wireless profiles installed on the computer. After a person checks off the profiles
they want export into an xml file netsh saves that file to the XML settings export
path chose back on Form1.
Figure 1 – Form 1 Design
Figure 2 – Profiles Form Design
III. The Code
First, I will go over the importation code which is very simple. The import SSID and
Network Profile Information button (also called BtnImport) imports the wireless
information from XML information files. Below in Figure 3, I show the
BtnImport_click
event code that does multiple item importation in one click. In
Figure 4, I show the BtnExport_Click
event which opens the Profiles Form and allows
the user to select the profiles to export. In Figure 5, I am loading the profiles in the
Profiles_Load event which fills the checkedlistbox with items. In Figure 6, I show the
code for BtnSelectProfiles_Click
event (the only button in figure 2).
Figure 3 – BtnImport_Click event
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
Dim result As System.Windows.Forms.DialogResult
OpenFileDialog1.Filter = "Xml Files (*.xml)|*.xml;"
OpenFileDialog1.Multiselect = True
result = OpenFileDialog1.ShowDialog()
If result = Windows.Forms.DialogResult.OK Then
If OpenFileDialog1.FileName <> "" Then
Dim importprocess As Process = New Process
importprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
If OpenFileDialog1.FileNames.Length >= 2 Then
For j As Integer = 0 To OpenFileDialog1.FileNames.Length - 1
importprocess = Process.Start("c:\Windows\System32\netsh.exe", _
"wlan add profile file=" + Chr(34) + _
OpenFileDialog1.FileNames.GetValue(j) + Chr(34) + " user=current")
Next j
Else
TxtOpenPath.Text = OpenFileDialog1.FileName
importprocess = Process.Start("C:\Windows\System32\netsh.exe", _
"wlan add profile file=" + Chr(34) + _
OpenFileDialog1.FileName + Chr(34) + " user=current")
System.Threading.Thread.Sleep(2000)
End If
End If
End If
End Sub
Figure 4 – BtnExport_Click event
Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
Dim result As System.Windows.Forms.DialogResult
result = FolderBrowserDialog1.ShowDialog()
If result = Windows.Forms.DialogResult.OK Then
If FolderBrowserDialog1.SelectedPath <> "" Then
TxtSavePath.Text = FolderBrowserDialog1.SelectedPath
TxtSavePath.Enabled = False
Profiles.Show()
End If
End If
End Sub
Figure 5 – Profiles_Load Event
Private Sub Profiles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim tempstr As String
Dim tmpstr1 As String
Dim tmpstr2 As String
Dim i As Integer
Dim wifistart As ProcessStartInfo = New ProcessStartInfo
Dim wifiprofiles As Process = New Process
Dim baddata As String
Dim profilesread As StreamReader
wifistart.CreateNoWindow = True
wifistart.WindowStyle = ProcessWindowStyle.Hidden
wifistart.FileName = "netsh"
wifistart.Arguments = "wlan show profiles"
wifistart.UseShellExecute = False
wifistart.RedirectStandardOutput = True
wifiprofiles = Process.Start(wifistart)
Dim profilefile As StreamReader = wifiprofiles.StandardOutput
If File.Exists(Application.StartupPath + "\Profiles.wpro") Then
File.Delete(Application.StartupPath + "\Profiles.wpro")
End If
Dim profilewrite As StreamWriter = _
New StreamWriter(Application.StartupPath + "\Profiles.wpro", False)
Do Until wifiprofiles.StandardOutput.EndOfStream
profilewrite.WriteLine(profilefile.ReadLine)
Loop
profilewrite.Close()
wifiprofiles.StandardOutput.Close()
profilesread = New StreamReader(Application.StartupPath + "\Profiles.wpro")
Do While Not profilesread.EndOfStream
If i >= 9 Then
tempstr = profilesread.ReadLine
If tempstr.IndexOf("All User Profile") <> -1 Then
tmpstr2 = tempstr.Remove(tempstr.IndexOf("All"), 22)
lstNetworkProfiles.Items.Add(tmpstr2)
End If
If tempstr.IndexOf("Current User Profile") <> -1 Then
tmpstr1 = tempstr.Remove(tempstr.IndexOf("Current"), 22)
lstNetworkProfiles.Items.Add(tmpstr1)
End If
Else
baddata = Nothing
baddata = profilesread.ReadLine()
End If
i = i + 1
Loop
System.Threading.Thread.Sleep(3000)
End Sub
Figure 6 – BtnSelectProfiles_Click Event
Private Sub BtnSelectProfiles_Click(sender As Object, e As EventArgs) Handles BtnSelectProfiles.Click
Dim importprocess As Process = Process.Start("c:\Windows\System32\cmd.exe")
importprocess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
Dim i As Integer
System.Threading.Thread.Sleep(2000)
For i = 0 To lstNetworkProfiles.Items.Count - 1
If lstNetworkProfiles.GetItemChecked(i) = True Then
My.Computer.Keyboard.SendKeys("netsh wlan export profile name=" + _
Chr(34) + Trim(lstNetworkProfiles.Items.Item(i).ToString) + Chr(34) + _
" folder=" + Chr(34) + Form1.TxtSavePath.Text + Chr(34))
My.Computer.Keyboard.SendKeys("{Enter}")
Else
End If
Next
System.Threading.Thread.Sleep(2000)
End Sub