473,461 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Picture box and deleting file

Hello,

after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?

Thank you for your help.

Frank

Jun 14 '07 #1
6 15044
You have to dispose the Image instance in the PictureBox first.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"FrankB" <bo*******@iabg.dewrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
Hello,

after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?

Thank you for your help.

Frank

Jun 14 '07 #2
Hi Kevin,

thank you for your help. Unfortunately a Dispose on the image does not
change anything. There is still the same message box.

Perhaps my code helps to understand the problem:

pictureBoxMain.Image = Image.FromFile ( sFile );

...

pictureBoxMain.Image.Dispose ( );
pictureBoxMain.Image = null;

try
{
File.Delete ( sFile );
}
Sorry for bothering again...

Frank

Jun 14 '07 #3
On 14 jun, 07:19, "Kevin Spencer" <unclechut...@nothinks.comwrote:
You have to dispose the Image instance in the PictureBox first.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net

"FrankB" <boedde...@iabg.dewrote in message

news:11**********************@d30g2000prg.googlegr oups.com...
Hello,
after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.
Is there a way to "free" the file?
Thank you for your help.
Frank
the dispose is the way, you should try to call the GarbageCollector be
more shure that the reference is eliminated

Jun 14 '07 #4
On Thu, 14 Jun 2007 04:09:52 -0700, FrankB <bo*******@iabg.dewrote:
after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?
As long as the Image is still around somewhere (eg attached to the
PictureBox), you're not going to be able to do anything with the file. I
assume that this is because .NET has memory-mapped the file, so that the
backing store is the original file rather than the swap file. At least,
that's the only reason I can think of for it to keep the file locked after
creating the Image instance.

Anyway, the solution is to make a copy of the image before attaching to
the PictureBox:

using (Image image = Image.FromFile(...))
{
pictureBoxMain.Image = new Image(image);
}

// deleting original image file should work at this point in the code

Pete
Jun 14 '07 #5
"FrankB" <bo*******@iabg.dewrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
Hello,

after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?
Why not first read the file into a memory stream, then create the image from
that memory stream? In this way, you can close the file right after you have
read the image data into your memory stream.

Regards,
Christian Stapfer

Jun 14 '07 #6
If you have picture box pb you need to dispose not the box, but the image.
For me this works:

pb.Image.Dispose();
pb.Image = null;

and then Delete on file.

But you must be pretty sure no other application is having the file open

HTH
Alex

"FrankB" <bo*******@iabg.dewrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
Hello,

after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.

Is there a way to "free" the file?

Thank you for your help.

Frank

Jun 15 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: MA | last post by:
Hi all! I created a webservice that check folder for incoming and outgoing files. If a file exists in my inbox (incoming file) I read it and want to delete it. It looks like this: ...
2
by: Mark | last post by:
Hi, In my web application I have to let users to download files from web server. How can track or find out that a file was downloaded so I can delete it from web server? I'm coping file to...
0
by: Bandit | last post by:
I'm populating a gridview (called docList) with document info from a network folder like so: Sub Show_Files(ByVal inputDir As String) Dim strFileNamePath As String = inputDir Dim dirInfo As...
5
by: Dylan Parry | last post by:
Hi, I'm trying to upload some files via a form, and I have managed so far to save the files on the server in a temporary directory using: HttpPostedFile.SaveAs(string filename); This is a...
1
by: diyasher | last post by:
hello my code is in c#. i am using fileSystemWatcher class to watch event when file is deleted. event is fire when file is deleted, i want to stop deleting file, means when user want to delete...
46
by: OldBirdman | last post by:
What a mess this question is. I have spent 2 weeks trying to make it concise and clear, and I can't. I do not have the vocabulary for the question. I cannot even TITLE it correctly. Here is my...
1
by: radhikabista | last post by:
my windows application form is divided into 3 split containers or split panels now what is my problem is i need to print the contents of the middle one container even if it is not seen full. but i...
2
Nepomuk
by: Nepomuk | last post by:
Recently, I found that I had associated a file type to notepad by mistake. This file type (let's just say, it has the extension .xyz) wasn't supposed to be opened by notepad - actually, it wasn't...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.