File access error

  • Thread starter Thread starter DavidC
  • Start date Start date
D

DavidC

I have an asp.net web page that reads lines from a csv file into a database
and then deletes the file when successful. The import works fine, but when I
go to delete it using the code below I am getting the error "The process
cannot access the file 'D:\databases\fapay\RP Imports\filename.csv' because
it is being used by another process." Is there a way around this? Thanks.

Dim fi2 As FileInfo = New FileInfo(fileName)
fi2.Delete()
 
I have an asp.net web page that reads lines from a csv file into a database
and then deletes the file when successful.  The import works fine, but when I
go to delete it using the code below I am getting the error "The process
cannot access the file 'D:\databases\fapay\RP Imports\filename.csv' because
it is being used by another process."  Is there a way around this?  Thanks.

            Dim fi2 As FileInfo = New FileInfo(fileName)
            fi2.Delete()

David,

It looks like you are opening the file for write access and then
attempting to delete it. Ensure that you close after import and try to
open as read-only

e.g. File.Open(path, FileMode.Open, FileAccess.Read)

Hope this helps
 
Hello David,
I have an asp.net web page that reads lines from a csv file into a
database
and then deletes the file when successful. The import works fine, but
when I
go to delete it using the code below I am getting the error "The process
cannot access the file 'D:\databases\fapay\RP Imports\filename.csv'
because
it is being used by another process."

Sounds like you have an open handle to the file. This might be because of
the import code you are using. Are you closing the file? Can you share the
code that does the import, as this might help us solve your issue?

There might be other issues here affecting the file deletion as well, like
virus scanning software, but this sounds unlikely as the file import works.
So I'd check the code first.
Dim fi2 As FileInfo = New FileInfo(fileName)
fi2.Delete()

By the way, you could also use the static Delete method of the File class to
delete the file. This way, you don't need to construct the FileInfo object
(unless you need it to something else, too).

Hope this helps.

--
Regards,

Jani Järvinen
C# MVP
Vantaa, Finland
E-mail: (e-mail address removed)
Business: http://www.nimacon.net/
Personal: http://www.saunalahti.fi/janij/
 
Back
Top