473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 15041
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.