Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 26th, 2008, 01:58 AM
Authorized User
 
Join Date: Aug 2008
Location: manila, , Philippines.
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();

                }


Reply With Quote
  #2 (permalink)  
Old August 26th, 2008, 02:05 AM
Authorized User
 
Join Date: Aug 2008
Location: Pune, , India.
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
Reply With Quote
  #3 (permalink)  
Old August 26th, 2008, 02:44 AM
Authorized User
 
Join Date: Aug 2008
Location: manila, , Philippines.
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #4 (permalink)  
Old August 26th, 2008, 02:47 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
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 -/
Reply With Quote
  #5 (permalink)  
Old August 26th, 2008, 06:03 AM
Authorized User
 
Join Date: Aug 2008
Location: manila, , Philippines.
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)

Reply With Quote
  #6 (permalink)  
Old August 26th, 2008, 06:50 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
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 -/
Reply With Quote
  #7 (permalink)  
Old August 26th, 2008, 10:41 AM
Authorized User
 
Join Date: Nov 2006
Location: Valparaiso, IN, USA.
Posts: 94
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!
Reply With Quote
  #8 (permalink)  
Old August 27th, 2008, 12:07 AM
Authorized User
 
Join Date: Aug 2008
Location: manila, , Philippines.
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...
Reply With Quote
  #9 (permalink)  
Old August 27th, 2008, 10:51 AM
Authorized User
 
Join Date: Nov 2006
Location: Valparaiso, IN, USA.
Posts: 94
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!
Reply With Quote
  #10 (permalink)  
Old August 28th, 2008, 05:29 AM
Authorized User
 
Join Date: Aug 2008
Location: manila, , Philippines.
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...
:)

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 01:03 PM.


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