views:

1193

answers:

3

I have 2 images inside a folder called Pics..... Image1.jpg and Image2.jpg.

What code must i place inside my Submit button to just delete Image1.jpg located here "~/Pics/Image1.jpg"

Any help would be great!!!

+1  A: 

You need to use System.IO.File.Delete not System.IO.Delete

string path = "~/Pics/Image1.jpg";
System.IO.File.Delete(Server.MapPath(path))
DrG
Thanks for the reply, it does work. But what must i do in code to loop through all my images inside my Pics folder and then delete all the images that contains a 1 before the extension such as *1.*
Etienne
You can use something like System.IO.Directory.GetFiles("~/Pics", "*1.*") to return an array of matching files in that directory. Then you can loop through those files and delete each one.
Lance McNearney
A: 

i would try:

String FilePath;
FilePath = Server.MapPath("~/Pics/Image1.jpg");
File.Delete(FilePath);
Baget
+2  A: 

The syntax is:

System.IO.File.Delete(Server.MapPath("~/Pics/Image1.jpg"));

You will need to make sure the user your web app is running as has delete (change) permissions on the file you are deleting, however.

Jason Williams
How would i give such permissions?
Etienne
It depends on your host's setup, but you will need to figure out which user your app is running as and change the permissions on the "Pics" folder on the disk. Usually this is done through some kind of control panel provided by your host. If you have terminal or physical access to the machine, you can right-click on the folder and visit the security tab to make these changes.
Jason Williams