473,771 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ 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 15092
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.goo glegroups.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...@n othinks.comwrot e:
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.goo glegroups.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 GarbageCollecto r 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.goo glegroups.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.Dispos e();
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.goo glegroups.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
2180
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: foreach(FileInfo fname in folder.GetFiles()) { if(fname.Exists) {
2
1041
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 a web server from shared drive and I have several web servers behind load balancer so if user
0
1409
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 DirectoryInfo = New DirectoryInfo(strFileNamePath) Dim aFiles As FileInfo() = dirInfo.GetFiles("*.doc") Array.Sort(aFiles, New CompareFileInfoEntries (CompareByOptions.LastWriteTime)) docList.DataSource = aFiles
5
7053
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 necessary stage as I need to perform some tests on the file before allowing it to be saved to its final destination directory. The files being uploaded contain data, so it's necessary to test the
1
2424
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 file, file cannot be deleted. how to stop deleting file bye
46
10099
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 best effort. Conditions: 80,000+ pictures (These are pictures of birds, ID by Species, Sex, Age, etc. (If known)) Hierarchy of folders: Continent->Country->State(if India, Mexico, USA, Canada, & Australia) (No state for other countries) I am...
1
8671
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 get only the part of the panel that is visible. for example in excel sheet we can get the whole sheet content even it is in minimized condition. but at point copyFromScreen(), i get the picture of the parts of panel theta is visible and other...
2
4517
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 supposed to be opened by anything at all. However, when I tried to remove the file type association like I could in previous versions of Windows, I found that I was meeting a dead end. I could change the program, no problem at all, but removing it...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.