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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:41:25+00:00 2026-06-12T20:41:25+00:00

So I am using C# and am making an enrollment system. So there is

  • 0

So I am using C# and am making an enrollment system. So there is an option to add a picture and what I do, I prompt the user to pick a file and that file will then be Copied to the Directory folder and then renamed into the student’s admission number. Here is the code for that browse button:

OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "All JPEG files (*.jpg; *.jpeg)| *.jpg; *.jpeg";
            string filter = openDlg.Filter;
            openDlg.Multiselect = false;
            openDlg.Title = "Open a JPG File";
            if (openDlg.ShowDialog() == DialogResult.OK)
            {                
                curFileName = openDlg.SafeFileName;
                string curFilePath = openDlg.FileName;

                openDlg.Dispose();

                string sourcePath = @curFilePath.Remove((curFilePath.Length - curFileName.Length));
                string targetPath = "@";

                mycon.Open();
                string cmdstr = "SELECT imageDirectory from userSettings WHERE ID = 1";
                cmd = new OleDbCommand(cmdstr, mycon);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    targetPath = (@dr["imageDirectory"].ToString());
                }
                dr.Close();
                mycon.Close();

                string sourceFile = Path.Combine(sourcePath, curFileName);
                string destFile = Path.Combine(targetPath, curFileName);

                File.Copy(sourceFile, destFile, true);

                newname = @destFile.Remove((destFile.Length - curFileName.Length)).ToString() + "\\" + (DateTime.Now.Year + "-" + textBox1.Text+".jpeg");

                if (File.Exists(newname) == true)
                {
                    pictureBox1.Image.Dispose();
                    try
                    {
                        File.Delete(newname);
                    }

                    catch (IOException ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                }

                File.Move(destFile, newname);

                photoPath = newname;
                pictureBox1.Image = Image.FromFile(photoPath);

The problem are:

a.) I have a function to allow the user to go to the next step and then if he wants to do some changes in the last step he could go back and update it. The problem here is when he changes the picture, I get an error stating “The file cannot be accessed because it is being used by another process”

b.) When the user already uploaded a picture and then went back to the home page, he won’t be able to upload a new picture when he decides to enroll again as well with an error stating: “The file cannot be accessed because it is being used by another process”.

The two errors both point here :

`File.Delete(newname);`

I really don’t know what to do guys I have been looking for a solution to this since last night and I can’t see a solution that wouldn’t get me to change the whole code altogether. please help 🙁

  • 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-12T20:41:27+00:00Added an answer on June 12, 2026 at 8:41 pm

    Your code is correct, ignore the put-down about code smell. Disposing the PictureBox.Image is important to avoid out-of-memory exceptions.

    This problem is caused by an implementation detail of the Bitmap class. When you open an image file, the class creates a memory-mapped file to map the pixel data of the image into memory. This was an important optimization for large images, it keeps the pixel data out of the paging file. It is not quite as important these days, 15 years after GDI+ was designed, modern machines have lots of RAM and disk space.

    That memory-mapped file however creates a lock on the file. Important to prevent any other process with messing with the file data and invalidating the mapped view of the file. Which is what you are seeing, trying to replace or delete the file no longer works. You avoid it by creating a deep copy of the bitmap, a copy that has all of the pixel data in memory and no longer uses the file. Like this:

    public static Bitmap LoadBitmapNolock(string path) {
        using (var img = Image.FromFile(path)) {
            return new Bitmap(img);
        }
    }
    

    Do note that the odds of running out of memory with large images on a 32-bit version of Windows is significantly increased with this code.

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

Sidebar

Related Questions

I'm making dynamic radio buttons by using ASP.NET. Where there a validation control that
I am using django and making a system similar to job portal or linkedin
I am making an application using LWUIT. There is a form There is a
We have a system wherein numerous retrieval/updation operations can be performed using making http
Every program that I'm making using pygtk is right-alignment by default (maybe because my
What is the biggest limitation of making/using webapps? I'm thinking that when you use
I'm making webpage using facebook API. I want to show user's friends in webpage.
Is there any way to detect whether a certain page is making noise using
I am working on android project and am making using of a ListView that
Right now, im using drupal for making a site. Im modifying the drupals user

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.