FileInfo.Delete fails to remove file

A

Allen

When using FileInfo to delete a file, the file remains in the directory (not
deleted)

Dim foo as New FileInfo(strFileToDelete)
Try
foo.Delete()
Catch ex as IOException
'Open file handler
End Catch

The file exists and is valid. The path exists and is valid. No file
attributes are set except for ARCHIVE. I have permission to read and write
to the directory.
No exception is thrown (ex = Nothing) yet the file is not deleted from the
directory.
Any suggestions?
Thanks in advance.
Allen
 
K

Ken Tucker [MVP]

Hi,

1) try using ex as exception instead of ioexception.
2) What do you get for foo.exists ?
3) Last option is File.Delete(strFileToDelete)

Ken
 
H

Herfried K. Wagner [MVP]

* "Allen said:
When using FileInfo to delete a file, the file remains in the directory (not
deleted)

Dim foo as New FileInfo(strFileToDelete)
Try
foo.Delete()
Catch ex as IOException
'Open file handler
End Catch

The file exists and is valid. The path exists and is valid. No file
attributes are set except for ARCHIVE. I have permission to read and write
to the directory.
No exception is thrown (ex = Nothing) yet the file is not deleted from the
directory.

Does it work if you use 'System.IO.File.Delete(<filename>)'?
 
O

One Handed Man [ OHM# ]

I can confirm that this does NOT work and that no exception is thrown. I
dont know why though. It may be a chaching issue ?

Regards - OHM
Does it work if you use 'System.IO.File.Delete(<filename>)'?

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
H

Herfried K. Wagner [MVP]

* "One Handed Man said:
I can confirm that this does NOT work and that no exception is thrown. I
dont know why though. It may be a chaching issue ?

You are referring to the 'FileInfo' class?
 
A

Allen

To All
The try catch exception is ex = Nothing (no exception is thrown)
foo.Exists = True

I shall try File.Delete(strFileToDelete) although this step is somewhat
counter intuitive when dealing with an inherited class such as FileInfo
(from base class File).

Regards,
Allen
 
O

One Handed Man [ OHM# ]

Funny enough though, I cant get that to work either, whats going on ?
To All
The try catch exception is ex = Nothing (no exception is thrown)
foo.Exists = True

I shall try File.Delete(strFileToDelete) although this step is
somewhat counter intuitive when dealing with an inherited class such
as FileInfo (from base class File).

Regards,
Allen

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
O

One Handed Man [ OHM# ]

When I tried this I was testing it with a file named test.txt, the system
appended the type .txt so the real filename was called 'test.txt.txt'

hence it did not work. Since then I have re-tried it with FileInfo.Delete()
and File.Delete("filename.txt") and they both work fine.


Regards - OHM

To All
The try catch exception is ex = Nothing (no exception is thrown)
foo.Exists = True

I shall try File.Delete(strFileToDelete) although this step is
somewhat counter intuitive when dealing with an inherited class such
as FileInfo (from base class File).

Regards,
Allen

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 

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

Top