2 Replies - 2706 Views - Last Post: 13 June 2011 - 07:40 AM Rate Topic: -----

#1 Sungori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 11

Problem copying files from pc to USB drive showing Progress

Posted 07 June 2011 - 08:26 AM

I am very new to programming and I am having trouble getting my code to work properly.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem copying files from pc to USB drive showing Progress

#2 Sungori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 11

Re: Problem copying files from pc to USB drive showing Progress

Posted 07 June 2011 - 10:00 AM

By the way, I did not mention that I plan on running the program from the flash drive. If that makes a difference...

Thanks again.

This post has been edited by Sungori: 07 June 2011 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 Sungori  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 11

Re: Problem copying files from pc to USB drive showing Progress

Posted 13 June 2011 - 07:40 AM

Im not sure why I did not get a response to my questions. Maybe it is because my code is just that terrible. Well I kept trying to get this to work and this is what I have come up with so far.

I have changed up the code using suggestions from things I found in this forum and others across the web including information found on the MSDN site. On the button click I have the following code:

Private Sub CreateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateButton.Click
        CreateButton.Enabled = False
        Me.Cursor = Cursors.WaitCursor
        Dim selectedFiles As New List(Of String)()
        MainLabel.Text = "Removing old installation files. Please be patient."
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(finishpath, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            selectedFiles.Add(foundfile)
        Next
        MainLabel.Text = "Copying new installation files. Please be patient, this will take several minutes."
        ProgressBar1.Visible = True
        ProgressBar1.Enabled = True
        BGWorker.RunWorkerAsync(selectedFiles)
    End Sub



I have the background worker doing this:

Private Sub BGWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWorker.DoWork
        Dim files As List(Of String) = CType(e.Argument, List(Of String))
        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
        If Me.BGWorker.CancellationPending Then
            e.Cancel = True
        End If
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        CopyFiles(worker, e)
    End Sub



And I have the CopyFiles function doing this:

Function CopyFiles(ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)
        Dim CurrentFileCopied As Integer = 0
        Dim theFiles() As String = System.IO.Directory.GetFiles(pcpath, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
        For Each currentFile As String In theFiles
            My.Computer.FileSystem.CopyFile(currentFile, finishpath, True)
            If worker.CancellationPending Then
                e.Cancel = True
            Else
                Dim percentComplete As Integer = (CurrentFileCopied / theFiles.Length) * 100
                If percentComplete > percentage Then
                    percentage = percentComplete
                    worker.ReportProgress(percentComplete)
                End If
            End If
            CurrentFileCopied += 1
        Next
    End Function



I would appreciate feedback to know if there is a better way to do any of this. I have no training in programming and am going just on what I have been able to teach myself, so please be patient with me and help me if you can.

Thanks,
Vernon
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1