Hi guys i have problem in my code when i try to copy image jpg from local disk in any place to network path DOMAIN
it give me error the process cannot access the file it using by another process file

why this message show

and how to solve.the error show in line delete of file

my code as below

private void button11_Click_1(object sender, EventArgs e)
{

openFileDialog1.Title = "Insert an image ";
openFileDialog1.InitialDirectory = "c:";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;      
pictureBox2.ImageLocation = openFileDialog1.FileName;
 }

private void button12_Click_4(object sender, EventArgs e)
{
const string new_dir = "\\\\192.168.1.5\\Personal Pictures";
MessageBox.Show(openFileDialog1.FileName);
string extension = System.IO.Path.GetExtension(openFileDialog1.FileName);
string renamed_name = textBox1.Text + "" + extension;
string fName = System.IO.Path.Combine(new_dir, renamed_name);
if (File.Exists(fName))
File.Delete(fName);this line give error
System.IO.File.Copy(openFileDialog1.FileName, fName);
string msg = string.Format("Copied {0} to {1}", openFileDialog1.FileName, fName);
MessageBox.Show(msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
How to solve the error
the exception error found in line delete

Dani AI

Generated

Quick diagnosis and where this usually comes from: the IOException means some process holds an exclusive handle on that file so File.Delete fails. As suggested, Windows Explorer (preview/thumbnail pane) can hold a handle; WinForms image APIs can also keep the file open after you load it (your use of pictureBox2.ImageLocation is relevant). The PictureBox/Image loading behavior and GDI+ locking are documented — the file can remain locked until the Image is disposed. (ftp.zx.net.nz)

Safe immediate fix (avoid locking the source file): load the file into a detached in‑memory Bitmap and use that in the PictureBox. Example pattern (C#):

Bitmap LoadDetachedBitmap(string path)
{
    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
    using (var tmp = Image.FromStream(fs))
    {
        return new Bitmap(tmp); // detached copy you can keep and dispose later
    }
}

When assigning, dispose the previous image first so your app doesn't keep the handle:

var old = pictureBox2.Image;
pictureBox2.Image = LoadDetachedBitmap(sourcePath);
old?.Dispose();

The FromStream pattern plus creating new Bitmap(tmp) is the reliable way to release file handles immediately. (learn.microsoft.com)

Overwrite vs explicit delete: instead of deleting then copying, use the overload that allows overwrite so you avoid a separate delete step (still note: if the destination really is locked, overwrite will throw too). Also wrap operations in try/catch for IOException so you can report which path failed and why. (learn.microsoft.com)

If the problem persists after releasing the PictureBox image, find which process holds the file: use Resource Monitor (Associated Handles) or Process Explorer (Find → Find Handle or DLL) to locate and release the handle. On network shares also check permissions, another user/service on the server, antivirus or backup agents that may hold files briefly. ()

Summary: release or replace the PictureBox image before touching the file, load images as detached Bitmaps (FromStream + new Bitmap), use File.Copy overload to overwrite, and use Process Explorer/Resource Monitor if a third party is locking the file.

Recommended Answers

All 2 Replies

give me error the process cannot access the file it using by another process file

Are you perhaps viewing the folder your file is in with the Windows Explorer?

Thank you for reply
ican viewing by windows explorer
i can delete it manually
and ican read write manually

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.