troubleshooting Question

Help with accessing excel file

Avatar of vcharles
vcharles asked on
.NET ProgrammingVisual Basic.NET
2 Comments1 Solution251 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros