Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:25:50+00:00 2026-06-10T15:25:50+00:00

When I’m trying to delete the images uploaded by me via website named SampleApplication

  • 0

When I’m trying to delete the images uploaded by me via website named “SampleApplication” I can see the following error shown in Stack Trace

 The process cannot access the file 'D:\Hosting\123456\html\App_Images\myfolder1\eKuK2511.png' because it is being used by another process.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: The process cannot access the file 'D:\Hosting\123456\html\App_Images\myfolder1\eKuK2511.png' because it is being used by another process.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[IOException: The process cannot access the file 'D:\Hosting\123456\html\App_Images\myfolder1\eKuK2511.png' because it is being used by another process.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +9723350
   System.IO.File.Delete(String path) +9545728
   SampleApplication.BasePage.DeleteApp_ImagesById(DataTable dt) +503
   SampleApplication.PostLease.MyAccount.DeleteAd_Property(Object sender, EventArgs e) +193
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +113
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

The folder App_Images has been given Read/write Permission and inherits to child folders namely myfolder1, myfolder2, myfolder3, myfolder4. Even when I tried to forcefully delete the image eKuK2511.png from the FTP File manager it is showing the following error:

550 The process cannot access the file because it is being used by another process. 

How to getrid of this error?

Edit:
Upload Code:

public void UploadImages()
        {
            if (ServerSideValidation() == true)
            {
                string SavePath;
                ImgPaths = new List<string>();
                // Get the HttpFileCollection
                HttpFileCollection hfc = Request.Files;
                if (hfc.Count > 0)
                {
                    for (int i = 0; i < hfc.Count; i++)
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            #region trials compression.
                            SavePath = "~/App_Images/" + Session["AppContext"].ToString() + "/" + GetUniqueKey() + GetFileExtension(hpf.FileName);
                            SaveImageByCompressing(hpf, SavePath);

                            #endregion
                            //SavePath can be saved in DB.
                            ImgPaths.Add(SavePath);
                            //Save Thumbnail Image.
                            if (i == 0)
                            {
                                string savedName = "Thumb_" + GetUniqueKey() + GetFileExtension(AppDomain.CurrentDomain.BaseDirectory + ImgPaths[0].ToString().Replace("~/", "\\").Replace("/", "\\"));
                                SavePath = "~/App_Images/" + Session["AppContext"].ToString() + "/" + savedName;
                                SaveThumbImage(AppDomain.CurrentDomain.BaseDirectory + ImgPaths[0].ToString().Replace("~/", "").Replace("/", "\\"), AppDomain.CurrentDomain.BaseDirectory + "App_Images\\" + Session["AppContext"].ToString() + "\\" + savedName, 75, 75);
                                ImgPaths.Add(SavePath);
                            }
                        }
                    }
                    Session.Remove("AppContext");
                    lblMsg.Text = "Images Uploaded Successfully.";
                    //ShowUploadedImages(ImgPaths);
                }
                else
                {
                    lblMsg.Text = "Images uploaded are either in wrong format or were deleted after uploading.";
                }
            }
            else
            {
                lstPaths = new List<string>();
                lblMsg.Text = "No Images Uploaded";
            }
        }

        private void SaveImageByCompressing(HttpPostedFile hpf, string filePath)
        {
            Image imgFromClient = Image.FromStream(hpf.InputStream);
            string SavetoFullPath = AppDomain.CurrentDomain.BaseDirectory + filePath.Replace("~/", "").Replace("/", "\\");
            Image.GetThumbnailImageAbort myCallbackCompressed = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Image imageToSave= imgFromClient.GetThumbnailImage(imgFromClient.Width, imgFromClient.Height, myCallbackCompressed, IntPtr.Zero);
            imageToSave.Save(SavetoFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);

        }
        public static void SaveThumbImage(string imagePath, string filePath, int width = 0, int height = 0)
        {
            Image originalImage = Image.FromFile(imagePath);
            if (width > 0 && height > 0)
            {
                Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                Image imageToSave = originalImage.GetThumbnailImage(width, height, myCallback, IntPtr.Zero);
                imageToSave.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            else
            {
                originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
        private static bool ThumbnailCallback() { return false; }

        private bool ServerSideValidation()
        {
            string errorMsg = string.Empty, temp = null;
            bool errorFlag = true;

            // Get the HttpFileCollection
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0 && hpf.FileName!=String.Empty)
                {
                    temp = ValidateImage(hpf);
                    if (temp != null)
                    {
                        errorMsg += GetFileName(hpf.FileName.ToString()) + " has error : " + temp;
                        temp = null;
                    }
                }
                else
                {
                    return false;
                }
            }

            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                lblMsg.Text = errorMsg;
                errorFlag = false;
            }
            return errorFlag;
        }

        private string GetFileExtension(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);
            return fi.Extension;
        }

        private string GetFileName(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);
            return fi.Name;
        }

        private string GetUniqueKey()
        {
            int maxSize = 8;
            char[] chars = new char[62];
            string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

            chars = a.ToCharArray();

            int size = maxSize;
            byte[] data = new byte[1];

            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();

            crypto.GetNonZeroBytes(data);
            size = maxSize;
            data = new byte[size];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(size);

            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length - 1)]);
            }
            return Session["AppContext"].ToString() + Page.User.Identity.Name.ToString() + result.ToString();
        }

        private string ValidateImage(HttpPostedFile myFile)
        {
            string msg = null;
            //6MB
            int FileMaxSize = 6291456;
            //Check Length of File is Valid or Not.
            if (myFile.ContentLength > FileMaxSize)
            {
                msg = msg + "File Size is Too Large. You are allowed only a maximum of 6MB per Image.";
            }
            //Check File Type is Valid or Not.
            if (!IsValidFile(myFile.FileName))
            {
                msg = msg + "Invalid File Type.";
            }
            return msg;
        }

        private bool IsValidFile(string filePath)
        {
            bool isValid = false;

            string[] fileExtensions = { ".BMP", ".JPG", ".PNG", ".GIF", ".JPEG" };

            for (int i = 0; i < fileExtensions.Length; i++)
            {
                if (filePath.ToUpper().Contains(fileExtensions[i]))
                {
                    isValid = true; break;
                }
            }
            return isValid;
        }

Delete Code:

/// <summary>
        /// Delete all images. If there are no Images then by default NoImage.png is assigned. So skip deleting that image.
        /// </summary>
        /// <param name="dt"></param>
        protected void DeleteApp_ImagesById(DataTable dt)
        {
            if (dt.Rows[0][0].ToString() != "~/images/NoImage.png")
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (dt.Rows[0][i].ToString() != string.Empty)
                    {
                        string str = Regex.Replace(dt.Rows[0][i].ToString(), "~/", "");
                        File.Delete(Request.PhysicalApplicationPath.ToString() + Regex.Replace(str, "/", "\\").ToString());
                    }
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:25:52+00:00Added an answer on June 10, 2026 at 3:25 pm

    The docs on MSDN about Image.Save say that the file remains locked until the Image is disposed.

    So I suppose that changing the code that save the images in this way

        private void SaveImageByCompressing(HttpPostedFile hpf, string filePath) 
        { 
            using(Image imgFromClient = Image.FromStream(hpf.InputStream))
            { 
                string SavetoFullPath = AppDomain.CurrentDomain.BaseDirectory + 
                                        filePath.Replace("~/", "").Replace("/", "\\"); 
                Image.GetThumbnailImageAbort myCallbackCompressed = 
                                        new Image.GetThumbnailImageAbort(ThumbnailCallback); 
                using(Image imageToSave= imgFromClient.GetThumbnailImage(imgFromClient.Width,
                                        imgFromClient.Height, myCallbackCompressed, IntPtr.Zero))
                {
                     imageToSave.Save(SavetoFullPath, System.Drawing.Imaging.ImageFormat.Jpeg); 
                }
            }
        } 
    
    
        public static void SaveThumbImage(string imagePath, string filePath, 
                                          int width = 0, int height = 0)   
        {   
            using(Image originalImage = Image.FromFile(imagePath))
            {
                if (width > 0 && height > 0)   
                {   
                    Image.GetThumbnailImageAbort myCallback = 
                          new Image.GetThumbnailImageAbort(ThumbnailCallback);   
                    using(Image imageToSave = originalImage.GetThumbnailImage(width, height, 
                                        myCallback, IntPtr.Zero))
                    {
                        imageToSave.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);   
                    }
                }   
                else   
                {   
                    originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);   
                }   
            }   
         }
    

    is a safe way to ensure your images are immediately unlocked when you finish with the upload code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.