Avatar of vcharles
vcharles 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

Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
vcharles

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jacques Bourgeois (James Burger)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
vcharles

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes