Help with uploading excel file

Hi,

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
            strm.Close()
            '  MessageBox.Show("Click Save")
        End If
    End Sub

Button_Click event:

   Try
            '  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"
             OpenFileDialog1.ShowDialog()
            Dim vc As String
            vc = "\"
            ' ch = 0
            DestinationPath = IO.Path.Combine(Application.StartupPath & vc, IO.Path.GetFileName(TextBox3.Text))
            MsgBox(DestinationPath)
            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
            Else
                IO.File.Copy(TextBox3.Text, DestinationPath)
            End If
   

Open in new window


Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
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"
        OpenFileDialog1.ShowDialog()
    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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.