Rapid File.Delete(Filename) causes UnauthorizedAccessException

  • Thread starter Thread starter Lucas Tam
  • Start date Start date
L

Lucas Tam

I have a very simple loop:

If (Directory.Exists(tempDirectory)) Then
Try
Dim Files() As String = Directory.GetFiles(tempDirectory)
'Clear out directory
For Each Filename As String In Files
File.Delete(Filename)
Next
Catch ex As Exception
End Try
End If

The first file will delete fine, but the second file will raise a
UnauthorizedAccessException.

To solve the problem, I added a Thread.Sleep(1000) after the File.Delete.

Anyone notice that if you delete stuff too quickly, .NET will raise a
UnauthorizedAccessException? Is there a way to continue after previous
delete is completed?
 
Lucas,
The first file will delete fine, but the second file will raise a
UnauthorizedAccessException.
I am not able to reproduce the error on Windows XP SP2 on a local drive, can
you give more specifics on where the tempDirectory you are trying to delete
from exists (a network drive for example).

Anyone notice that if you delete stuff too quickly, .NET will raise a
UnauthorizedAccessException?
As I stated above, no problems here...
Is there a way to continue after previous
delete is completed?
Put the Try Catch inside the For Each Loop.
For Each Filename As String In Files Try
File.Delete(Filename)
Catch ex As Exception
Debug.WriteLine(ex, "File.Delete")
End Try

Hope this helps
Jay
 
As I stated above, no problems here...

Hmmm... I think I found the problem. A component I was using was not
releasing the file.

HOWEVER, for some reason I could delete the file via Windows Explorer. But
the same file IS NOT deleted by VB.NET...

I wonder if Windows Explorer is using a different delete method than
VB.NET???
 
According to UnauthorizedAccessException Class. I guess you didn't have
permission to delete. Right click the file--> property-->Security--> Set
"Full control" for youself. Try again.

Hopes help
 
I can't reproduce your problem. Here's a sample that works fine for me. Is
there something special about the folder that you're deleting from?

Imports System.IO

Module Module1

Sub Main()
Directory.CreateDirectory("c:\test")
For i As Integer = 0 To 99
Console.WriteLine("Creating: " & "c:\test\" & i & ".txt")
Dim fs As FileStream = File.Create("c:\test\" & i & ".txt")
fs.Close()
Next

For Each filename As String In Directory.GetFiles("c:\test")
Console.WriteLine("Deleting: " & filename)
File.Delete(filename)
Next

Console.ReadLine()
End Sub

End Module

- Scott Swigart
http://blog.swigartconsulting.com
 
Lucas,
I wonder if Windows Explorer is using a different delete method than
VB.NET???
Yes & No.

My understanding File.Delete simply calls the Win32 DeleteFile API. At least
that is what I would expect it to call, I haven't check the rotor source or
ILDASM.

While my understanding is Windows Explorer calls SHFileOperation, which I
expect calls the Win32 MoveFile or MoveFileEx API to move the file to the
Recycle Bin or Win32 DeleteFile API to physically delete it.

I would expect when Windows Explorer empties the Recycle Bin that the Win32
DeleteFile API is then called...

Hope this helps
Jay
 
I can't reproduce your problem. Here's a sample that works fine for
me. Is there something special about the folder that you're deleting
from?

I think I figured out the progrm... a component I was using was holding the
file.

However, Windows Explorer could delete the file.

Meanwhile, VB.NET "deletes" the file on the first round but doesn't remove
it from the directory. If the delete method is called again, a
UnauthorizedAccessException is thrown. The file is finally deleted when the
application is shutdown.

It seems that an ActiveX component I am using has some sort of handle on
the file. It doesn't block delete function but rather silently swollows
them until the component is shut down.

Very strange...
 
Lucas,

You are deleting from a collection which gives at me very often this kind of
problems.

Did you alreaydy try this one
For Each Filename As String In Files
File.Delete(Filename)
Next
For i as integer is Files.length -1 to 0 step -1
Files(i).delete
Next

Just a guess and typed in this message not tried.

Cor
 
Cor,
You are deleting from a collection which gives at me very often this kind
of problems.
I actually had the same thought initially.

However! He is not however removing elements from the Files collection.

The Files collection is an array of Strings, of file names.

File.Delete is removing the file from the hard drive, it does not remove it
from the array.

Hope this helps
Jay
 
Jay,

You are right. I had seen many solutions in this treath and not this one,
so I had to think on this, however it was a black out from me.

I tried by the way to simulate the problem and did not get any exception.

I have put that in the original thread

Cor
 
Lucas,

I have tried your sample and had not any problem. Not that it is important
however the way you make a Try, Catch and End Try block is in my opinion
against everything it should be.

\\\
Imports System.IO
Public Class main
Public Shared Sub main()
For i As Integer = 1 To 100
Dim tempdirectory As String = "C:\test3\"
If (Directory.Exists(tempdirectory)) = False Then
Directory.CreateDirectory(tempdirectory)
End If
Dim p As New Process
Dim pi As New ProcessStartInfo
pi.UseShellExecute = False
pi.Arguments = "C:\windows\*.* C:\Test3\"
pi.FileName = "xcopy.exe"
p.StartInfo = pi
p.Start()
p.WaitForExit()
If (Directory.Exists(tempdirectory)) Then
Try
Dim Files() As String =
Directory.GetFiles(tempdirectory)
For Each Filename As String In Files
File.Delete(Filename)
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End If
Next
End Sub
End Class
///

The processstart way is because I have seen another message today in this
newsgroup with a problem about this, so I could test that in one time.

I hope this helps something to try for others.

Cor
 
I have tried your sample and had not any problem. Not that it is
important however the way you make a Try, Catch and End Try block is
in my opinion against everything it should be.

It seems that a component I am using is locking the file from being deleted
- but I can move/rename the file.

When I delete the file, it queues the file to be deleted. A second call to
delete the file will throw an exception (UnauthorizedAccessException). Kind
of strange... is there a way to force a release on the file?
 
Lucas,

I remember me that I had the same problems as you are telling in a
multithreading operation, you are not using that by the way.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top