Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 26th, 2008, 12:58 AM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default Unable to delete file System.IO.Delete error

I have a program that creates a directory (C:\\Temp) where image files are being saved. My form closing event will clean up the image files, however it throws an exception..”The process cannot access the file x.jpg because it is being used by another process”..When I open the temp folder almost all of files saved there are deleted except for one(x.jpg)..How can I stop the “another process” from locking this file…
I used this code:


            foreach (string picturePath in System.IO.Directory.GetFiles(“C:\\Temp”, "*.jpg"))
            {

                try
                {
                    Process[] myProcesses;
                    myProcesses = Process.GetProcessesByName("Outlook");
                    foreach (Process myProcess in myProcesses)
                    {
                        myProcess.CloseMainWindow();
                    }

                    FileStream fs = File.Open(picturePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete);
                    File.Delete(picturePath);
                    fs.Close();
                    fs.Dispose();

                }


 
Old August 26th, 2008, 01:05 AM
Authorized User
 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i am not sure but you can try by assigning NULL value to each process
lets see if it works

~vikas
 
Old August 26th, 2008, 01:44 AM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I tried it..nothing happend..any other suggestions...

 
Old August 26th, 2008, 01:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If windows says another process still has the file open, then a process still has the file open. This might even be your program.

There error will not be in the deleting code, but more likely in the creating code, or in the fact a program is using the files. You don't say what you are doing with the images.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 26th, 2008, 05:03 AM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I want to delete these images because they keep on replicating each time
i run my program..These are actually outlook attachments..(contacts)..I used a SaveAsFile command,that's why the files are saved and stored in C:\\temp folder..When i close my form i want these images to be removed also..I am also wondering why the code can delete the files but always leaves just one file..(the one thrown by the Exception)

 
Old August 26th, 2008, 05:50 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you download an application called FileMon (from the Microsoft web site) it can hopefully tell you which program has the file locked. Then you can try and work out why it has it locked.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 26th, 2008, 09:41 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

If you separate the class that creates and uses the images from the one that deletes the images, could you force the garbage collection on the creating/using class by calling dispose() on it? (by implementing IDisposable in the class).

What you don't know can hurt you!
 
Old August 26th, 2008, 11:07 PM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

 I already figured out what process locks my images...Using filemon i've found out that OUTLOOK locks the images (which my current program), i want to stop this process from using my images but don't want to terminate it..(since i'm creating this addin for outlook)...when i issue kill..outlook terminates immediately and no longer proceeds to file deletion..Is there a way to stop outlook from using my files so i can delete my file..without closing it.Thanks for the replies...
 
Old August 27th, 2008, 09:51 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Why is Outlook using the image? I'm guessing you open or create or save the image using the Outlook application somewhere in your code. Typically an addin will need to open a connection to it's host application. If you create a class that does this and open an instance of the class for each image you want to process then dispose of the instance when you are through with the image, you may be able to delete the image.

What you don't know can hurt you!
 
Old August 28th, 2008, 04:29 AM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Garbage Collection issue..thanks for the reply..i can already delete the images...

System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(picturePath);

Gave me headache for 3 days...
:)






Similar Threads
Thread Thread Starter Forum Replies Last Post
php/mysql delete button and delete query dungey PHP Databases 17 April 11th, 2009 12:24 PM
How can I delete a file doctorsom Beginning PHP 2 July 28th, 2005 02:29 PM
File Delete qazi_nomi Classic ASP Basics 1 May 17th, 2005 06:54 PM
how to delete a row when click delete hyperlink naveenkumarg1 Pro JSP 1 August 16th, 2004 01:29 AM
Delete A File jmss66 VB How-To 5 August 10th, 2004 11:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.