You asked for an example, so we go. Imagine you want to delete an image saved in "/pics/path/mypic.jpg". You must call first
string path = Server.MapPath("/pics/path/mypic.jpg");
to get the physical path of the image and save it in a variable path. Then you can do:
FileInfo fi = new FileInfo(path); if(fi.Exists) fi.Delete(); else what you want to do if file does'nt exists.
Note: fi.Delete () does not throw an exception if the file does not exist.
To get a list of all the files in a directory, you can use Directory.GetFiles () or an instance of DirectoryInfo.
source share