Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with accessing excel file

Hello,

When attempting to access an excel file I receive the following error evnthough it is not being used by another application:

The process cannot access the file 'parh\Debug\DataA.xls' because it is being used by another process.

on line:
  IO.File.Copy(TextBox2.Text, destinationPathWithSourceName, True)

How do I avoid this error?

 If IO.File.Exists(destinationPathWithSourceName) Then
            If MessageBox.Show("Destination already exists!" & vbCrLf & vbCrLf & "File: " & destinationPathWithSourceName, "Replace File?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                IO.File.Copy(TextBox2.Text, destinationPathWithSourceName, True)
                If File.Exists(destinationPathWithDestName) Then    'Delete Data.xls if it already exists
                    My.Computer.FileSystem.DeleteFile(destinationPathWithDestName)
                End If
                My.Computer.FileSystem.RenameFile(destinationPathWithSourceName, "Data.xls")
                ' namesheet()
                makeDataTableFromSheetNameUpload()
                loadgrid()
            End If
        Else
            IO.File.Copy(TextBox2.Text, destinationPathWithSourceName)
            If File.Exists(destinationPathWithDestName) Then    'Delete Data.xls if it already exists
                My.Computer.FileSystem.DeleteFile(destinationPathWithDestName)
            End If
            My.Computer.FileSystem.RenameFile(destinationPathWithSourceName, "Data.xls")
            'namesheet()
            makeDataTableFromSheetNameUpload()
            loadgrid()
            Label3.Visible = True
            ComboBox2.Visible = True
        End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor  Charles

ASKER

Hi,

It's certainly not the code because the error doesn't occur when  publish the application. The file doesn't show on the task, must be something going on with my latop.

Thanks,

V.