| | |
Deleting File Through C# Program.
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hi,
In my application i am taking multiple image files from user using openfiledialog and display those image files in flowlayoutpanel. After pressing on Insert button i used to copy all those images in a folder (C:\Images). When there is no file exists the file get copied easily, but if it exists i delete the file, but while deleting file it shows error
"The process cannot access the file"C:\Images\xyz.jpg" because it is being used by another process"
The code i used is
if (File.Exists(destFileName))
{
File.Delete(destFileName);
File.Copy(sourceFileName, destFileName);
}
else
File.Copy(sourceFileName, destFileName);
Is it because i display the images in flowlayoutpanel while deleting. But before execting above code i clears all controls added in the flowlayoutpanel. Still the error occurs.. What should i do?
Regards,
Vidya
In my application i am taking multiple image files from user using openfiledialog and display those image files in flowlayoutpanel. After pressing on Insert button i used to copy all those images in a folder (C:\Images). When there is no file exists the file get copied easily, but if it exists i delete the file, but while deleting file it shows error
"The process cannot access the file"C:\Images\xyz.jpg" because it is being used by another process"
The code i used is
if (File.Exists(destFileName))
{
File.Delete(destFileName);
File.Copy(sourceFileName, destFileName);
}
else
File.Copy(sourceFileName, destFileName);
Is it because i display the images in flowlayoutpanel while deleting. But before execting above code i clears all controls added in the flowlayoutpanel. Still the error occurs.. What should i do?
Regards,
Vidya
0
#2 Oct 7th, 2009
Your process is the one that uses file, you need to set image to null use something like this,
C# Syntax (Toggle Plain Text)
using(var img = Image.FromFile(filename).GetThumbnailImage(GetWitdth(filename, GetHeight(filename, 200)), GetfHeight(filename, 200), null, new IntPtr())) pictureBox1.Image = img;
0
#3 Oct 7th, 2009
I use the following code to add pictureboxes,
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = 245;
pb.Width = 245;
pb.BorderStyle = BorderStyle.None;
pb.Image = loadedImage;
pb.Name = "pic" + j.ToString();
pb.Click += new EventHandler(picture_click);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
flowLayoutPanel1.Controls.Add(pb);
Should i do something like this,
loadedImage=null;
Regards,
Vidya
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = 245;
pb.Width = 245;
pb.BorderStyle = BorderStyle.None;
pb.Image = loadedImage;
pb.Name = "pic" + j.ToString();
pb.Click += new EventHandler(picture_click);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
flowLayoutPanel1.Controls.Add(pb);
Should i do something like this,
loadedImage=null;
Regards,
Vidya
0
#4 Oct 7th, 2009
If you want to repalce the file if it exists, rather than deleting it then copying it, try changing it to this:
The boolean at the end sets whether files can be replaced. By setting it to true it should overwrite the file if it already exists
C# Syntax (Toggle Plain Text)
File.Copy(sourceFileName, destFileName, true);
The boolean at the end sets whether files can be replaced. By setting it to true it should overwrite the file if it already exists
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
•
•
If you want to repalce the file if it exists, rather than deleting it then copying it, try changing it to this:
C# Syntax (Toggle Plain Text)
File.Copy(sourceFileName, destFileName, true);
The boolean at the end sets whether files can be replaced. By setting it to true it should overwrite the file if it already exists
Sorry, but still error occurs,
But the error occurs only if the image(the path which i want to delete) is loaded in the flowlayoutpanel.
Means, if the image is loaded in the flowlayoutpanel it is not deleted.

Vidya
1
#6 Oct 7th, 2009
had a quick search around the Micrsoft Support site, if you use FromFile in code to set an image file to a picturebox it locks the file. You can avoid this by using a filestream to retrieve the image:
C# Syntax (Toggle Plain Text)
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read); PictureBox1.Image = System.Drawing.Image.FromStream(fs); fs.Close();
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
0
#8 Oct 7th, 2009
Have you implemented the code i gave you? This solves the problem as it reads the file without locking it.
Your code should look like this:
Your code should look like this:
C# Syntax (Toggle Plain Text)
PictureBox pb = new PictureBox(); System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read); Image loadedImage = Image.FromStream(fs); fs.Close(); pb.Height = 245; pb.Width = 245; pb.BorderStyle = BorderStyle.None; pb.Image = loadedImage; pb.Name = "pic" + j.ToString(); pb.Click += new EventHandler(picture_click); pb.SizeMode = PictureBoxSizeMode.StretchImage; flowLayoutPanel1.Controls.Add(pb);
Last edited by Ryshad; Oct 7th, 2009 at 7:57 am.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
0
#9 Oct 7th, 2009
Aah, no problem.
I take it the code fixed the issue then? If so, remember to mark the thread as solved.
I take it the code fixed the issue then? If so, remember to mark the thread as solved.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- Deleting a file, having problems w compling (C++)
- Error Deleting File or Folder (Windows NT / 2000 / XP)
- Error deleting file or folder (Viruses, Spyware and other Nasties)
- Error deleting file or folder (Windows NT / 2000 / XP)
- error deleting file (Windows NT / 2000 / XP)
- PLEASE HELP about Using C# to add an executable file to my program! (C#)
- Lost all file program associations (Windows NT / 2000 / XP)
- Unknown file/program (Viruses, Spyware and other Nasties)
- Syntax for deleting specified file ( in C++) (C++)
Other Threads in the C# Forum
- Previous Thread: Live video streaming
- Next Thread: c# nse File.Delete cannot delete "Temporary Internet Files" files,help me
Views: 338 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gdi+ http httpwebrequest image index input install java label list listbox login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml






