ahmed sa

ahmed sa

  • NA
  • 289
  • 125.5k

It is using by another process when delete file open in pict

Nov 25 2014 1:27 PM
Hi guys i have proplem when i press copyto button to copy file from path to path
it give me error it is using by another process and give me exception in
file.delete in code
my code copy file to the path successfully
only give me error exception when i copy file exist in path destination(the path i transfer to)
suppose i need to copy file name 111 and this not exist in destination it success
but if i need to copy file name 111 and this exist in destination it give me error
Exception it using by another process and exception come in line
file.delete(path)
how to solve proplem
button browse not give me any error and success in browse
only wrong error display when i press copyto button
my code as following
Note: itry to set picturebox1 to null but also proplem show
 

private void Browse_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Title = "Insert an image ";
openFileDialog1.InitialDirectory = "::{208D2C60-3AEA-1069-A2D7-08002B30309D}";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;
fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
pictureBox1.ImageLocation = openFileDialog1.FileName;
fs.Close();
catch (Exception ex)
{
MessageBox.Show(ex + "Please");
}
}

private void CopyTo_Click(object sender, EventArgs e)
{
try
{
const string new_dir = "\\\\192.168.1.5\\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))
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(fName);the exception display in this line
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);
}
catch (Exception ex)
{
MessageBox.Show(ex + "Error");
}
}


Answers (1)