Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,080 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,585 people online right now. Registration is fast and FREE... Join Now!




Problem deleting file - access denied

 
Reply to this topicStart new topic

Problem deleting file - access denied, file in use when trying file.delete()

atrum_tutela
14 Apr, 2008 - 01:43 PM
Post #1

New D.I.C Head
*

Joined: 21 Feb, 2008
Posts: 4



Thanked: 1 times
My Contributions
I'm using the fileSystemWatcher in a windows service to detect when an xml file is dropped into an outbox directory. If the file dropped in there is not an xml file I want to delete it. Most of the time when I drop a small (0 - 1K) file in there that does not have a .xml extension it is deleted with no problem. If the file is larger (>5-6K) I get an exception stating that file access is denied when it tries to delete. blink.gif
CODE

string fileName = e.Name.ToString();
                FileInfo newFile = new FileInfo(fileName);
                string fileExt = newFile.Extension;
                
                if (fileExt != ".xml")
                {
                    newFile.Delete();
                    File.Delete(outboxPath + "\\" + fileName);
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine(DateNow + "- " + e.Name.ToString() + " is an invalid file type. Removed from outbox");
                        sw.Close();
                        sw.Dispose();
                    }
                }


I either need to be able to check to see if the file is open or be able to close it before deleting but have not been able to figure out how to do either... mad.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:37PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month