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

I asked this question some days ago. but no such reply came that can solve my problem.
My code when i run on local system, deletes files from the local machine. when i run the same code online (Internet). Despite of read write access in the folder where i am deleting the file. it creates exception that the file is in use.
My code to delete that files are as under. The code is simple. I have references in the database to pictures in the directory.All the pictures in the directory are uploaded using the system. I have to get the references which i have to delete for both the thumbnail image and full images from the database and repose both of the references in list
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(); // Complete paths for all full images to be deleted
 var thmbImagePaths = new List(); // Complete paths for all thumbnail images to be deleted
 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(); } }


So kindly if any solution available, kindly guide me.



Regards
TanzeelurRehman
Posted
Updated 23-Mar-12 7:34am
v3
Comments
Sergey Alexandrovich Kryukov 5-Mar-12 0:08am    
What is that: "online server"?
--SA
Sergey Alexandrovich Kryukov 5-Mar-12 0:09am    
Did you run it under debugger? Just before deletion, did you check up that a file really exists at the absolute path calculated before deletion? Any exception?
--SA
Tech Code Freak 5-Mar-12 0:40am    
Try adding this below if() block:
else
{
//throw some Exception.
}

Insert some breakpoints and debug your program to know what is happening actually.
TanzeelurRehman 5-Mar-12 2:05am    
When the picture is in use in html markup on another page, will that page to be close before deleting the picture. Because now i am running the application and it is working accurately. a day before it was throwing exception that the file is in use, so couldnot be deleted
shreekar 23-Mar-12 14:21pm    
Using the picture in the HTML mark up is completely unrelated to the permissions issue.
Please check with the web host provider whether permissions were changed between then and now.

1 solution

Its Permission Issue, You can add Identity Impersonation in your web.config and IIS will let your application deletes those files.

C#
<identity impersonate="true">
          userName="domain\user" 
          password="password" /></identity>
 
Share this answer
 

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

  Print Answers RSS


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