VB.NET: Using Netsh to import and export Wireless Settings
Introduction
Wireless settings can be exported and imported using netsh.
In this article, we 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.
References
Source code is located here: http://jeffsblogcodesamples.codeplex.com in the downloads section under Exporting and Importing Netsh Wireless Settings
This is an authorised copy of the forum post from codeproject here: http://www.codeproject.com/Articles/696520/Exporting-and-Importing-Wireless-Settings-Using-Ne.
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 the 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 (could also call it Wifi Backup Form)
http://www.codeproject.com/KB/vb/696520/form1.jpg
Figure 2 - Profiles Form Design (or Profile selection form)
http://www.codeproject.com/KB/vb/696520/profile.jpg
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 or single profile 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
'only import xml files because that is all the command will accept.
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.CreateNoWindow = True
importprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
'importprocess.StartInfo.UseShellExecute = False
' importprocess.StartInfo.RedirectStandardOutput = True
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
Description:
The BtnImport_Click event imports wireless profile information from an XML file to your
computer to avoid having to perform a procedure like reentering a wireless password
or setting up settings to connect to a wireless network.
This includes but is not limited to: wireless passphrase, security type, encryption type, etc.
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 IfEnd Sub
Description:
The BtnExport_Click event allows you to choose what folder the profile
information will be stored in and launches the profiles form which does
the job of exporting the information to xml files.
Profiles_Load Event
Private Sub Profiles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Dim Profiles As List(Of String) = New List(Of String)
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)
' MsgBox("All user profile replace:" + tmpstr2)
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)
' MsgBox("current user profile replace:" + tmpstr1)
Description:
The Profile_Load event saves all the profile names of profiles that could be exported to the file "Profiles.wpro". Then it adds them to a checked list box to be selected.