Delete file


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
 
Ad

Advertisements

M

Matt

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
 
K

Kerem Gümrükcü

Hi Miguel,

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

Regards

Kerem
 
P

Pavel Minaev

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()!
 
Ad

Advertisements

M

Matt

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
 

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

Similar Threads

Path 2
Load XML file 3
Class and XML file. Very strange problem. 1
XML ID ... 7
Token Expiration Date. How? 11
String 3
Trim String 9
Why "delete" file hang? 1

Top