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 8389409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:43:08+00:00 2026-06-09T18:43:08+00:00

In my current project I’m facing random errors complaining the file in question is

  • 0

In my current project I’m facing random errors complaining the file in question is being used by other processes.

Sometime it acts fast and everything is just fine, some other times it won’t work and keeps giving me the error

The file is being used by another process

So I thought I place the delete method inside a try-catch and in the catch I have some kind of loop which would try to delete the file or even better: unlocks it and makes it ready for being deleted.

I have no idea if inside that loop I get another exception, and how to manage that. How can I find a solution to detach that process from the file, and make it ready for deletion?

Update

This is my code at the moment:

 private void Test(string imagePath)
 {
        try
        {
            if (File.Exists(imagePath))
            {
                pictureBoxExistingPicture.Visible = true;
                labelnew.Visible = true;
                labelold.Visible = true;
                pictureBoxExistingPicture.Image = ImageUtility.SafeLoadImage(imagePath);

                if (MessageBox.Show("Do you want to overwrite the existing image?", "warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    pictureBoxExistingPicture.Image = Properties.Resources._default;//restore default

                    while (true)
                    {
                        try
                        {
                            File.Delete(imagePath);
                            break;
                        }
                        catch (Exception)
                        {
                        }
                    }

                    pictureBoxHonarjo.Image.Save(imagePath);
                }
                else
                {
                    pictureBoxHonarjo.Image = ImageUtility.SafeLoadImage(imagePath);//restore original image
                }

                pictureBoxExistingPicture.Visible = false;
                labelnew.Visible = false;
                labelold.Visible = false;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

in the current context of the subject, i need to say that if the user wishes to replace the image, it should get done , and it shouldn’t take a long time nor should it fail to do so.So basically when someone tries to change a picture,It must get changed in a fraction of the time.

And for the SafeLoadImage function, here is my implementation:

public class ImageUtility
{
   public static byte[] ImageToBytes(string FilePath, int FileLength)
   {
      FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
      BinaryReader breader = new BinaryReader(fileStream);
      return breader.ReadBytes(FileLength);
    }

    public static Image SafeLoadImage(string imagePath)
    {
        byte[] image_byte;
        Image image;
        FileInfo fileinfo = new FileInfo(imagePath);
        image_byte = ImageUtility.ImageToBytes(imagePath, (int)fileinfo.Length);
        image = ImageUtility.BytesToImage(image_byte);
        return image;
    }
}
  • 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-09T18:43:10+00:00Added an answer on June 9, 2026 at 6:43 pm

    ImageUtility.ImageToBytes doesn’t dispose the FileStream when finished

    Could it be this that’s locking the file?

    public static byte[] ImageToBytes(string FilePath, int FileLength) 
    { 
        FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read); 
        BinaryReader breader = new BinaryReader(fileStream); 
        return breader.ReadBytes(FileLength); 
    }
    

    Consider the using statement which will free resources after execution exits the code block (it wraps IDisposable and calls the dispose method for you):

    public static byte[] ImageToBytes(string FilePath, int FileLength) 
    { 
        using(FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read)) 
        {
            BinaryReader breader = new BinaryReader(fileStream); 
            return breader.ReadBytes(FileLength); 
        }
    }
    

    This may be the reason your file is locked.

    Of course if someone else is locking your file, then this might be useless 🙂 but I suspect that these are image files you have created/are processing

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

Sidebar

Related Questions

My current project requires me to assemble a .zip file containing HTML and text-only
My current project has a .js file with hard coded data in it. So
My current project used tables for layout design and I am trying to convert
My current project involves deploying an upgraded .exe file that runs as a Windows
My current project is using a metadata file to set properties without having to
In my current project, I have to create a PDF file on the fly
The current project in wherein I'm working creates a log file using the log4j
My current project is broken down into 3 parts: Website, Desktop Client, and a
Our current project has ran into a circular dependency issue. Our business logic assembly
My current project involves using LDAP (Active Directory) and I'm using issue tracking for

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.