?
Solved

file fails to copy durring form load vb.net

Posted on 2014-04-17
4
Medium Priority
?
490 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 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 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 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 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