Solved

file fails to copy durring form load vb.net

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now