By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

Rapid File.Delete(Filename) causes UnauthorizedAccessException

P: n/a
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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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 Next
Hope this helps
Jay
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #2

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in news:
#X**************@TK2MSFTNGP14.phx.gbl:
Anyone notice that if you delete stuff too quickly, .NET will raise a
UnauthorizedAccessException?

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

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #3

P: n/a
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

"Lucas Tam" wrote:
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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #4

P: n/a
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 Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #5

P: n/a
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
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. . "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in news:
#X**************@TK2MSFTNGP14.phx.gbl:
Anyone notice that if you delete stuff too quickly, .NET will raise a
UnauthorizedAccessException?

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

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #6

P: n/a
"Scott Swigart" <sc***@3leaf.com> wrote in
news:##**************@TK2MSFTNGP09.phx.gbl:
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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #7

P: n/a
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
Nov 21 '05 #8

P: n/a
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Ou**************@TK2MSFTNGP15.phx.gbl... 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

Nov 21 '05 #9

P: n/a
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com>
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Ou**************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #10

P: n/a
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

"Lucas Tam" <RE********@rogers.com>
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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #11

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in
news:u0**************@TK2MSFTNGP11.phx.gbl:
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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #12

P: n/a
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

"Lucas Tam" <RE********@rogers.com>

"Cor Ligthert" <no************@planet.nl> wrote in
news:u0**************@TK2MSFTNGP11.phx.gbl:
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 Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.