Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want delete a file but an error occured please help me.
==============================
The process cannot access the file 'D:\My Project\AzadUniv\New\12\GUI\Upload\NewsImage\5564868769930125853-Image' because it is being used by another process.
==============================


C#
string ImagePath = Server.MapPath(".")+"\\Upload\\NewsImage\\" + Utility.GenerateGUID() + "-Image";
            if (fuImage.PostedFile != null)
            {
                fuImage.PostedFile.SaveAs(ImagePath);
                fuImage.Dispose();
                string d = Utility.GetThumbnailImage(ImagePath, 100, 100,true);

            }



  public static string GetThumbnailImage(string path,int x,int y,bool DeleteAfterSave)
        {

                // Create the in-memory bitmap where you will draw the image.
                Bitmap image = new Bitmap(x, y);
                Graphics g = Graphics.FromImage(image);

                // Load the file data.
                System.Drawing.Image thumbnail =
                System.Drawing.Image.FromFile(path);

                // Draw the thumbnail.
                g.DrawImage(thumbnail, 0, 0, x, y);

                // Render the image.
                string NewPath = path +"-small.jpg";
               image.Save(NewPath, ImageFormat.Jpeg);
                
                g.Dispose();
                image.Dispose();
                if (DeleteAfterSave == true)
                {
                    System.IO.File.Delete(path);
                }
                return path;
            }
Posted
Updated 23-Jul-12 10:44am
v2
Comments
ZurdoDev 23-Jul-12 17:00pm    
What's the question? The error is telling you what is going on. If your code is correct you'll likely have to kick it off on a new thread and let the current thread finish, so that dispose is actually called.
Behnam Mohammadi 24-Jul-12 11:44am    
THIS IS A ANSWER!!!!!!!!!!!!!!!!!
You are attempting to delete the file that was read in within the thumbnail object. Perhaps if you disposed of the thumbnail object, the file resource would be released.

1 solution

You are attempting to delete the file that was read in within the thumbnail object. Perhaps if you disposed of the thumbnail object, the file resource would be released.
 
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