C# .NET - it is being by another process when copy file from local disk to folder in domain network

Asked By ahmed elbarbary on 05-Nov-14 07:31 AM
Hi guys i have problem in my code when i try to copy image jpg from local disk in any place to network path https://social.msdn.microsoft.com/Forums/vstudio/en-US/10882f26-d329-4560-9a1a-8b456b831684/it-is-being-by-another-process-when-copy-file-from-local-disk-to-folder-in-domain-network-why?forum=csharpgeneral#
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)
pictureBox2.ImageLocation = openFileDialog1.FileName;

private void button12_Click_4(object sender, EventArgs e)
const string new_dir = "\\\\\\Personal Pictures";
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
if possible help me

Robbe Morris replied to ahmed elbarbary on 05-Nov-14 08:18 AM
The picturebox control may have a lock on the file.  Set the source property that holds the image to null and see if that doesn't work.
ahmed elbarbary replied to Robbe Morris on 05-Nov-14 09:35 AM
Thank you for reply
it is not lock on file
lock to picture box control is false
and i take picture from path
can any one help me if possible
Robbe Morris replied to ahmed elbarbary on 05-Nov-14 09:41 AM
Unless you've worked with the image in some other piece of software and that still has a lock on the stream, this is almost always an issue with something in your application having not closed the stream on the image.  Sometimes, it is the picturebox.  Sometimes it is something else.

Can you manually move (not copy, move) the image to another folder?  Shut visual studio down completely before trying.  If so, you know it isn't an outside piece of software.

Wire up a small console app that does nothing but copy this file.  It doesn't open the image or capture a path from a dialog.  Just the 2 or three lines of code to copy it.  If that is successful, then you know for sure "somewhere" in your app is opening a stream on that image and it isn't getting close before attempting to copy it.