Solved

file fails to copy durring form load vb.net

Posted on 2014-04-17
4
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
Stimphy earned 300 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 12

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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

737 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