Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Every one

Below is my code to delete file from server. Locally i Tested it. It work fine for me.

C#
var fi = new FileInfo(Server.MapPath(thmbImagePath));
                  if (fi.Exists)
                  {
                     fi.Delete();
                  }


When i Tried this online (internet). It generates exception that could not be deleted file in use error.

So help is needed. What is the best way to delete online files.

Regards
TanzeelurRehman
Posted
Updated 1-Mar-18 20:31pm
Comments
walterhevedeich 2-Mar-12 2:00am    
I'm thinking it might be a security exception. Can you post the exact error message?
TanzeelurRehman 2-Mar-12 2:13am    
Sorry for now, later i can post the error. Now the server is down, we are doing some work on it.
As i know, the images are in use that's why it is giving error. pictures are uploaded on the location. Deletion is creating problems. it seems that temporary files for the images are created which are not allowing the pictures to be deleted. How to delete the temporary images first and where are they located. Just guide me in this scenario
OriginalGriff 2-Mar-12 3:28am    
It is probably to do with how you are handling the images - look at the code and check that you are closing and disposing all the file related objects - if you don't then the file can be in use until the GC gets in and clears them out. Which could be next week...

Dispose that file object wherever you are accessing that file, another issue might be user rights to the folder.
 
Share this answer
 
Comments
TanzeelurRehman 2-Mar-12 4:40am    
Thanks for your response.
I got that i have to dispose the image files where i am using. I have image gallery. Where i am using the links to the file images. Nothing more. how do i dispose the images where i have used them in links
Sushil Mate 2-Mar-12 4:53am    
can you please post the image gallery code snippet.
TanzeelurRehman 2-Mar-12 5:25am    
protected void grdAllCars_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var dataKey = grdAllCars.DataKeys[e.RowIndex];
var companyId = Utilities.GetCompanyId();
if (dataKey != null && companyId != null)
{
decimal cId = Convert.ToDecimal(companyId);
decimal CarId = Convert.ToDecimal(dataKey.Value.ToString());
var objContext = new CarShowRoomEntities();
var showRoomData = objContext.tbl_Cars.SingleOrDefault(car => (car.CarId == CarId && car.CompanyId == cId));
var allImages = objContext.tbl_Car_Image.Where(image => image.CarId == CarId).ToList();
var largeImagePaths = new List<string>();
var thmbImagePaths = new List<string>();
foreach (var img in allImages)
{
largeImagePaths.Add(img.CarImagePath);
thmbImagePaths.Add(img.CarThmb_ImagePath);
objContext.DeleteObject(img);
}
objContext.DeleteObject(showRoomData);
objContext.SaveChanges();
try
{
foreach (var thmbImagePath in thmbImagePaths)
{
var fi = new FileInfo(Server.MapPath(thmbImagePath));
if (fi.Exists)
{
fi.Delete();
}
}
foreach (var largeImagePath in largeImagePaths)
{
var fi = new FileInfo(Server.MapPath(largeImagePath));
if (fi.Exists)
{
fi.Delete();

}
}
}
catch
{

}

if (ViewState["SearchQuery"] != null)
PopulateGridViewBasedOnQuery(ViewState["SearchQuery"].ToString());
else
PopulateGridViewBasedOnQuery();
}
}
Sushil Mate 5-Mar-12 0:10am    
see don't use the var its quite confusing to understand your code, use the proper objects and assign them, after use dispose them. before deleting the file dispose all the objects related to image gallery. you are putting the reference or image in the gallery, you have to clear that thing reference & image before deleting the file. there are some objects are not getting freed.. that's why you are not able to delete it.
I tried the same code it's working fine. Please see folder permission.
C#
DirectoryInfo dr = new DirectoryInfo(Server.MapPath("TempPDF"));
        try
        {
            FileInfo[] files = dr.GetFiles();
            for (int i = 0; i < files.Length; i++)
                files[i].Delete();
        }
        catch (Exception ex) { //catch your exception here
}
 
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