File access error

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()
 
G

Guest

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
 
J

Jani Järvinen [MVP]

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top