p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   C# (http://p2p.wrox.com/c-22/)
-   -   Unable to delete file System.IO.Delete error (http://p2p.wrox.com/c/70255-unable-delete-file-system-io-delete-error.html)

maricar August 26th, 2008 12:58 AM

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();

                }



vikas67k August 26th, 2008 01:05 AM

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

~vikas

maricar August 26th, 2008 01:44 AM

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


samjudson August 26th, 2008 01:47 AM

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 -/

maricar August 26th, 2008 05:03 AM

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)


samjudson August 26th, 2008 05:50 AM

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 -/

David_0223 August 26th, 2008 09:41 AM

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!

maricar August 26th, 2008 11:07 PM

 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...

David_0223 August 27th, 2008 09:51 AM

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!

maricar August 28th, 2008 04:29 AM

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...
:)



All times are GMT -4. The time now is 03:54 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.