Help with uploading excel file


I am using code below to upload an excel file to my application's folder but receiving error message:"The  process can not access file \xxxxxxx\ because it is being used by another process"
on line:
    IO.File.Copy(TextBox3.Text, DestinationPath, True)

Also since distributing this application using clickonce technology, will the code upload the file to the application's folder?

How do I fix this error?

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim strm As System.IO.Stream
        strm = OpenFileDialog1.OpenFile()
        TextBox3.Text = OpenFileDialog1.FileName.ToString()
        If Not (strm Is Nothing) Then
            'insert code to read the file data
            '  MessageBox.Show("Click Save")
        End If
    End Sub

Button_Click event:

            '  Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\", IO.Path.GetFileName("\ExcelFileA.xls"))
            Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\")

            OpenFileDialog1.InitialDirectory = "C:temp"
            Dim vc As String
            vc = "\"
            ' ch = 0
            DestinationPath = IO.Path.Combine(Application.StartupPath & vc, IO.Path.GetFileName(TextBox3.Text))
            If IO.File.Exists(DestinationPath) Then
                If MessageBox.Show("Destination already exists!" & vbCrLf & vbCrLf & "File: " & DestinationPath, "Replace File?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    IO.File.Copy(TextBox3.Text, DestinationPath, True)
                End If
                IO.File.Copy(TextBox3.Text, DestinationPath)
            End If

Open in new window


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        OpenFileDialog1.Title = "Please Select a File"

        OpenFileDialog1.InitialDirectory = "C:temp"
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim SourceFile As String
        Dim destinationPath As String
        Dim destinationFile As String
        Dim dirName As String

        destinationPath = "C:\test\"

        TextBox1.Text = OpenFileDialog1.FileName.ToString()
        SourceFile = TextBox1.Text
        dirName = IO.Path.GetFileName(SourceFile)
        destinationFile = destinationPath + dirName
        If System.IO.File.Exists(SourceFile) = True Then
            System.IO.File.Copy(SourceFile, destinationFile)
        End If
        MessageBox.Show("File Transfered")

    End Sub
End Class

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.