Solved

file fails to copy durring form load vb.net

Posted on 2014-04-17
4
477 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

707 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