Delete file

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am deleting a file from database data as follows:
File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
String.Concat(s.SlidePath, s.SlideFilename)));

The problem is that I get an error when the file does not exist.

How can I prevent that?

Thanks,
Miguel
 
Hello,

I am deleting a file from database data as follows:
File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
String.Concat(s.SlidePath, s.SlideFilename)));

The problem is that I get an error when the file does not exist.

How can I prevent that?

The File.Exists method might be a good place to start :)

Try

if ( File.Exists(filename) )
File.Delete(filename);

where, of course, filename is the string you build above.
You'll find File.Exists in the System.IO assembly.

matt
 
Hi Miguel,

you first check with "File.Exists(...)"
and enwrap all potentially error raising
sections with a try/catch construct. Thats
it,...

Regards

Kerem
 
The File.Exists method might be a good place to start :)

Try

if ( File.Exists(filename) )
   File.Delete(filename);

where, of course, filename is the string you build above.
You'll find File.Exists in the System.IO assembly.

Of course, you still have to catch IOException on the call to
File.Delete even though you do the check - because another process
might have deleted the file between the call to Exists() and the call
to Delete()!
 
Of course, you still have to catch IOException on the call to
File.Delete even though you do the check - because another process
might have deleted the file between the call to Exists() and the call
to Delete()!

Excellent point. Of course, you could just catch the exception to
begin
with, and skip the Exists call, but I prefer to make it exceptions a
truly
"exceptional" event.

Matt
 
Back
Top