Michael Olivero
The official blog of Michael Olivero, Software Architect & Humble Entrepreneur

.NET Framework FileInfo.Delete() method throws UnauthorizedAccessException

Thursday, 1 March 2012 17:03 by Michael Olivero

When deleting a file using FileInfo.Delete() method, the .NET framework sometimes throws an exception as shown below:


Yet, when you delete the file manually through explorer its removed fine from the filesystem.  At first I though this was a glitch of some type because the program runs with my login credentials much like the local logged in user credentials applies to windows explorer.  It turns out if the file is marked as ReadOnly it also throws this exception.  Changing the attributes prior to deleting will automatically resolve this problem.

An additional note, since this program was working with a directory where files were continually being created and removed, there was an additional observation with the File.CreationTime.  In very few cases when iterating through the files in the directory, the CreationTime would show a date time value of 1/1/1600 despite the file already existing.  I attribute this to simply a race condition where the FileInfo.CreationTime of my program gets to the file just before the actual creation date of the file is set by the operating system. This would need to be validated as I would expect the creation time of the file to be set in an atomic way along with the file creation.

Tags:   ,
Categories:   Software
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed