I wrote a simple program that lets you select 1 to 6 (USB) drives, enter their drive letters, and copy a folder's contents to the drives.
I --KNOW-- there is a better way to code the Execute sub. But I am a pretty novice coder and this is what my brain came up with.
I know it's not good to call form objects in a BackgroundWorker. So what is a better way to do that in this example? Also, I have a progressbar in form2, and I can't get it to update the progressbar from the backgroundworker. It won't update til the worker completes, then the bar fills up. Anyway, here is the main UI and the code:
This is Form1, simple enough to understand
Imports System.ComponentModel
Public Class Form1
Public intDrives As Integer
Private Declare Function SetVolumeLabel Lib "kernel32" Alias "SetVolumeLabelA" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long
Private Function ChangeVolumeLabel(DriveLetter As String, NewDriveVolume As String) As Boolean
'Example:
'ChangeVolumeLabel("C:\", "CDrive")
Dim lAns As Long
On Error Resume Next
lAns = SetVolumeLabel(DriveLetter, NewDriveVolume)
ChangeVolumeLabel = (lAns <> 0)
End Function
Private Function DriveCheck(DriveLetter As String) As Boolean
Try
If IO.Directory.Exists(DriveLetter & ":\") Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Private Sub Execute()
Dim sourceDir As String = txtSourceDir.Text
intDrives = cboNumDrives.SelectedIndex + 1
Select Case cboNumDrives.SelectedIndex
Case 0
If txtDrive1.Text <> "" And DriveCheck(txtDrive1.Text) = True Then
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
If chkLabel.Checked Then
ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
End If
Else
cmdExecute.Enabled = True
MsgBox("Drives entered incorrectly or not found. Please try again.")
Exit Sub
End If
Case 1
If txtDrive1.Text <> "" And txtDrive2.Text <> "" And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True Then
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
If chkLabel.Checked Then
ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
End If
Else
cmdExecute.Enabled = True
MsgBox("Drives entered incorrectly or not found. Please try again.")
Exit Sub
End If
Case 2
If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True _
And DriveCheck(txtDrive3.Text) = True Then
Form2.Label1.Text = String.Format("Copying files to drive 3 of {0}...", cboNumDrives.SelectedValue.ToString)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
If chkLabel.Checked Then
ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
End If
Else
cmdExecute.Enabled = True
MsgBox("Drives entered incorrectly or not found. Please try again.")
Exit Sub
End If
Case 3
If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And txtDrive4.Text <> "" And DriveCheck(txtDrive1.Text) = True _
And DriveCheck(txtDrive2.Text) = True And DriveCheck(txtDrive3.Text) = True And DriveCheck(txtDrive4.Text) = True Then
Form2.Label1.Text = String.Format("Copying files to drive 4 of {0}...", cboNumDrives.SelectedValue.ToString)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive4.Text), True)
If chkLabel.Checked Then
ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive4.Text), txtLabel.Text)
End If
Else
cmdExecute.Enabled = True
MsgBox("Drives entered incorrectly or not found. Please try again.")
Exit Sub
End If
Case 4
If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And txtDrive4.Text <> "" And txtDrive5.Text <> "" _
And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True And DriveCheck(txtDrive3.Text) = True _
And DriveCheck(txtDrive4.Text) = True And DriveCheck(txtDrive5.Text) = True Then
Form2.Label1.Text = String.Format("Copying files to drive 5 of {0}...", cboNumDrives.SelectedValue.ToString)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive4.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive5.Text), True)
If chkLabel.Checked Then
ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive4.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive5.Text), txtLabel.Text)
End If
Else
cmdExecute.Enabled = True
MsgBox("Drives entered incorrectly or not found. Please try again.")
Exit Sub
End If
Case 5
If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And txtDrive4.Text <> "" And txtDrive5.Text <> "" _
And txtDrive6.Text <> "" And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True _
And DriveCheck(txtDrive3.Text) = True And DriveCheck(txtDrive4.Text) = True And DriveCheck(txtDrive5.Text) = True _
And DriveCheck(txtDrive6.Text) = True Then
Form2.Label1.Text = String.Format("Copying files to drive 6 of {0}...", cboNumDrives.SelectedValue.ToString)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive4.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive5.Text), True)
My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive6.Text), True)
If chkLabel.Checked Then
ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive4.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive5.Text), txtLabel.Text)
ChangeVolumeLabel(String.Format("{0}:\", txtDrive6.Text), txtLabel.Text)
End If
Else
cmdExecute.Enabled = True
MsgBox("Drives entered incorrectly or not found. Please try again.")
Exit Sub
End If
End Select
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles cmdExecute.Click
Label9.Visible = False
If BackgroundWorker1.IsBusy Then Exit Sub
If Not My.Computer.FileSystem.DirectoryExists(txtSourceDir.Text) Then
cmdExecute.Enabled = True
MsgBox("Please select a valid source directory.")
Exit Sub
End If
cmdExecute.Enabled = False
Dim form2 As New Form2
form2.Label1.Text = String.Format("Copying files to {0} drive(s)...", cboNumDrives.SelectedIndex + 1)
form2.Show()
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
cboNumDrives.SelectedIndex = 0
txtDrive1.Select()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If chkLabel.Checked Then
txtLabel.Enabled = True
Else
txtLabel.Enabled = False
End If
Select Case cboNumDrives.SelectedIndex
Case 0
txtDrive1.Enabled = True
txtDrive2.Enabled = False
txtDrive3.Enabled = False
txtDrive4.Enabled = False
txtDrive5.Enabled = False
txtDrive6.Enabled = False
Case 1
txtDrive1.Enabled = True
txtDrive2.Enabled = True
txtDrive3.Enabled = False
txtDrive4.Enabled = False
txtDrive5.Enabled = False
txtDrive6.Enabled = False
Case 2
txtDrive1.Enabled = True
txtDrive2.Enabled = True
txtDrive3.Enabled = True
txtDrive4.Enabled = False
txtDrive5.Enabled = False
txtDrive6.Enabled = False
Case 3
txtDrive1.Enabled = True
txtDrive2.Enabled = True
txtDrive3.Enabled = True
txtDrive4.Enabled = True
txtDrive5.Enabled = False
txtDrive6.Enabled = False
Case 4
txtDrive1.Enabled = True
txtDrive2.Enabled = True
txtDrive3.Enabled = True
txtDrive4.Enabled = True
txtDrive5.Enabled = True
txtDrive6.Enabled = False
Case 5
txtDrive1.Enabled = True
txtDrive2.Enabled = True
txtDrive3.Enabled = True
txtDrive4.Enabled = True
txtDrive6.Enabled = True
End Select
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btnSelectSource.Click
Dim fbd As New FolderBrowserDialog
'write new pat1h to IID database to line 1 of settings.ini
If fbd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtSourceDir.Text = fbd.SelectedPath
Else
Return
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Call Execute()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
cmdExecute.Enabled = True
End Sub
End Class