Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using gridview and in gridview i have a checkbox column and i using delete button to delete the Checked record. i am uploading images through asp.net fileUpload Server Control. and in a DataBase i am just Storing the Image Name with its Extension for ex Image1.jpg and same name i am storing in Images folder in my Application root path.
now problem is when i delete any record then that images should also delete from the Images folder from root path how to do that..?.

how to find perticular id and that file name from d'base and math with image name in Image Folder.
can any one give me a solution...?
Posted
Updated 21-Apr-13 8:10am
v2
Comments
[no name] 21-Apr-13 14:12pm    
What have you tried? SELECT your filename from the database and then use File.Delete to delete file.

I could have located the file and used File.Delete Method[^] to delete the file from directory.
Try this:
C#
System.IO.File.Delete("MyFilePath");


--Amit
 
Share this answer
 
Comments
[no name] 22-Apr-13 2:41am    
From your explanation I understood that you want to delete File from uploaded file storage folder.
In Delete click event get the file name and root path of images and delete it using below statement

System.IO.File.Delete("Your file root path with your file name");

If you have multiple files use looping statements.

My explanaiton completly based on unique file names in image storage folder.
_Amy 22-Apr-13 2:46am    
Yes, OP need to loop through the directory. See this:
foreach(string fileName in Directory.GetFiles("SomeDirectory"))
{
//your codes here to handle the files.
}


OR

string [] fileEntries = Directory.GetFiles(sourceDir);
foreach(string fileName in fileEntries)
{
// do something with fileName
Console.WriteLine(fileName);
}


--Amit
_Amy 22-Apr-13 2:54am    
Unique file names?
What to do with this? A folder cannot have two files with the same name. And also I can't find much difference in your solution. As OP is telling, filename will come from database, that has to be deleted. So, first check for the file whether it exits or not. If exists then delete.

--Amit
[no name] 22-Apr-13 2:59am    
yes I agree with you

forgot it man
Try the code given below:

C#
string completePath = Server.MapPath("YourFilePath");

if (System.IO.File.Exists(completePath))
{

System.IO.File.Delete(completePath);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900