Click here to Skip to main content
11,702,929 members (62,969 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Server file File-Upload , +
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.
==============================


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 23-Jul-12 10:16am
Edited 23-Jul-12 10:44am
v2
Comments
ryanb31 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 415
1 Sergey Alexandrovich Kryukov 325
2 OriginalGriff 260
3 CHill60 120
4 Andy Lanng 119
0 OriginalGriff 8,379
1 Sergey Alexandrovich Kryukov 7,587
2 CPallini 4,947
3 Maciej Los 4,501
4 Mika Wendelius 3,566


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 23 Jul 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