?
Solved

file fails to copy durring form load vb.net

Posted on 2014-04-17
4
Medium Priority
?
501 Views
Last Modified: 2014-04-21
Im trying to copy some files and 1 of them keep failing, but not sure why.  It starts the copy, but only copies 7kb instead of 580.  In this same sub I have 4 other files that copy fine.  What could I be missing?

This is failing

       'make sure Workstation Monitoring dir is there
        Try
            My.Computer.FileSystem.CreateDirectory("c:\temp\WorkstationMonitor")
        Catch ex As Exception

        End Try
        Application.DoEvents()
        System.Threading.Thread.Sleep(2000)
        'copy latest version of Workstation Monitoring
        Try
'*********next line fails!!!!! it only copies part of the file
            System.IO.File.Copy(scriptlocation & "Workstation Monitoring.exe", "c:\temp\WorkstationMonitor\Workstation Monitoring.exe", True)

        Catch ex As Exception
            MessageBox.Show("Failed to copy Workstation Monitoring.exe.  Update will exit", "Update Will Exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()

        End Try



but this works fine after the form loads and I press button1

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            System.IO.File.Copy(scriptlocation & "Workstation Monitoring.exe", "c:\temp\WorkstationMonitor\Workstation Monitoring.exe", True)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
0
Comment
Question by:chadmanvb
4 Comments
 
LVL 3

Accepted Solution

by:
Stimphy earned 1200 total points
ID: 40008209
I've seen this behavior before as well, I havn't found the exact cause, but you could copy the file using a thread.  When the form loads, start the thread.  The form will continue loading while your thread handles the file copy.  You would then need a delegate to handle the form related operations, like the message box you display.  Doing this allows the form to fully load whilst copying the file.

Such as:
    Dim thCopy As Threading.Thread
    Delegate Sub delegateDoNotify(e As Exception)
    Dim notify As New delegateDoNotify(AddressOf doNotify)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        thCopy = New Threading.Thread(AddressOf doFileCopy)
        thCopy.Start()
    End Sub

    Private Sub doFileCopy()
        Dim tEX As Exception = Nothing
        'make sure Workstation Monitoring dir is there
        Try
            My.Computer.FileSystem.CreateDirectory("c:\temp\WorkstationMonitor")
        Catch ex As Exception
        End Try
        System.Threading.Thread.Sleep(2000)
        'copy latest version of Workstation Monitoring
        Try
            System.IO.File.Copy(scriptlocation & "Workstation Monitoring.exe", "c:\temp\WorkstationMonitor\Workstation Monitoring.exe", True)
        Catch ex As Exception
            tEX = ex
        End Try
        If tEX IsNot Nothing Then
            'notify.BeginInvoke(tEX)
            Me.BeginInvoke(notify, New Object() {tEX})
        End If
    End Sub

    Private Sub doNotify(e As Exception)
        MessageBox.Show(Me, "Failed to copy Workstation Monitoring.exe.  Update will exit", "Update Will Exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Application.Exit()
    End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:ktaczala
ID: 40008288
in your first code why don't you display the exception?  You check for it but you display a generic message.  Have you examined the exception?  What is it?
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 800 total points
ID: 40008675
Very often, when you have something that does not work properly in the Load event, moving it to the VisibleChanged event solves the problem.
0
 

Author Closing Comment

by:chadmanvb
ID: 40012570
Still dont understand why this did not work, but both your ideas works.  Even in my try/catch it would not always give me an error, because it thought the copy was good.  Sometimes it did give me a "file not found" error.  Anyway, thanks for the help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question