I am trying to write what I thought was a simple program, but I everything I have tried has not worked properly.
Here is what I am trying to do...
I am creating a utility that will take installation and database files from one computer and copy those files to a flash drive so the flash drive will have the new version. The flash drives in question would either have one of two previous versions of the software on it or it will be blank. I decided to do this in two steps.
First I check the computer for the new database and the flash drive for the old ones. I do this with the following code.
Private Sub VerifyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerifyButton.Click VerifyButton.Enabled = False VerifyButton.Visible = False If System.IO.File.Exists(pcdatabase) = False Then MainLabel.Text = "This computer does not contain the files need to update this Flash Drive to the latest installation software." CloseButton.Enabled = False CloseButton.Visible = False FinishButton.Visible = True FinishButton.Enabled = True ElseIf System.IO.File.Exists(threedatabase) And System.IO.File.Exists(pcdatabase) Then MainLabel.Text = "Your Flash drive currently contains the " & (System.IO.Path.GetFileNameWithoutExtension(threedatabase)) & "database. This computer contains the" & (System.IO.Path.GetFileNameWithoutExtension(pcdatabase)) & "database. Press start to copy the new data to your flass Drive." CreateButton.Visible = True CreateButton.Enabled = True ElseIf System.IO.File.Exists(fourdatabase) And System.IO.File.Exists(pcdatabase) Then MainLabel.Text = "Your Flash drive currently contains the " & (System.IO.Path.GetFileNameWithoutExtension(fourdatabase)) & "database. This computer contains the" & (System.IO.Path.GetFileNameWithoutExtension(pcdatabase)) & "database. Press start to copy the new data to your flass Drive." CreateButton.Visible = True CreateButton.Enabled = True Else MainLabel.Text = "Your Flash drive does not currently contain any installation software. Press start to copy the" & (System.IO.Path.GetFileNameWithoutExtension(pcdatabase)) & "database to your flass Drive." CreateButton.Visible = True CreateButton.Enabled = True End If End Sub
By the way... I failed to mention that I have already "Dim"ed (don't know what is the right way to say that, sorry) all of the paths that I used in my code like this:
Dim pcpath As String = "<ALLUSERSPROFILE>\Application Data\ProgramFolder" Dim finishpath As String = "FlashFolder\ProgramFolder" Dim datafinishpath As String = "FlashFolder\ProgramFolder\DataFolder" Dim formuladata As String = "NewDatabase.bak" Dim threedatabase As String = " FlashFolder\ProgramFolder\DataFolder\Version3.bak" Dim fourdatabase As String = " FlashFolder\ProgramFolder\DataFolder\Version4.bak" Dim pcdatabase As String = "<ALLUSERSPROFILE>\Application Data\ProgramFolder\DataFolder\NewDatabase.bak"
So this works, but I feel like there must be a better way to do this. This is just the only way I have been able to get it working so any suggestions on better ways would be greatly appreciated.
Next, I am deleting files from the flash drive if the old database is there and copying the files from the computer to the flash drive and showing progress on the progressbar. Here is where I am going all wrong. I have tried it a number of different ways, and unfortunatly (I'm kicking myself right now), I have not saved the code from the other methods I have tried.
I have not been able to figure out how to get the background worker to work with what I am trying to do, though I have seen a lot of code over the past few weeks with examples of how to use it. It is a little above my head in terms of figuring it out by myself. So what I have used is the following code:
Private Sub CreateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateButton.Click CreateButton.Enabled = False CloseButton.Enabled = False CloseButton.Visible = False MainLabel.Text = "Removing old installation files. Please be patient." Me.Cursor = Cursors.WaitCursor ProgressBar1.Visible = True ProgressBar1.Enabled = True For Each foundfile As String In My.Computer.FileSystem.GetFiles(finishpath, FileIO.SearchOption.SearchAllSubDirectories, "*.*") My.Computer.FileSystem.DeleteFile(foundfile, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently) Next MainLabel.Text = "Copying new installation files. Please be patient, this will take several minutes." For Each newfile As String In My.Computer.FileSystem.GetFiles(pcpath, FileIO.SearchOption.SearchAllSubDirectories, "*.*") My.Computer.FileSystem.CopyDirectory(newfile, finishpath, FileIO.UIOption.AllDialogs) Next ProgressBar1.Enabled = False ProgressBar1.Visible = False FinishButton.Visible = True FinishButton.Enabled = True End Sub
The code works to delete the files, but I get an error on the copy that says:
"Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.
Could not find directory 'C:\Documents and Settings\All Users\Application Data\ProgramFolder\autorun.inf'."
It does not copy any files, and it just stops working. I am wondering if it is because the "ProgramFolder" is hidden and how would I get around that. I tried using System.IO.File.Copy() also, but I could not get that to work either. Also, the progress bar never shows up at all. I have it set to marquee, though I would prefer it to actually be acurate to the date being transferred.
Here are the things I think I need help with (though I'm almost sure there are other things):
1. A better way to check the computer and flash drive for the correct versions of the software
2. A way to copy the data from the computer to the flash drive
3. A way to show the actual progress (hopefully using a background worker because of reason 4)
4. I really want to use a background worker because I want to be able to create a cancel button (I tried this when working on the background worker before, but I never got it to actually do anything)
5. Whatever I don't realize I need.
Thanks for the help. I greatly appreciate it.