Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
Hi Every one
 
Below is my code to delete file from server. Locally i Tested it. It work fine for me.
 
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 1-Mar-12 20:49pm
Comments
walterhevedeich at 2-Mar-12 2:00am
   
I'm thinking it might be a security exception. Can you post the exact error message?
TanzeelurRehman at 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 at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Dispose that file object wherever you are accessing that file, another issue might be user rights to the folder.
  Permalink  
Comments
TanzeelurRehman at 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 at 2-Mar-12 4:53am
   
can you please post the image gallery code snippet.
TanzeelurRehman at 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();
var thmbImagePaths = new List();
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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I tried the same code it's working fine. Please see folder permission.
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
}
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150326.1 | Last Updated 2 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100