473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Rapid File.Delete(Filename) causes UnauthorizedAccessException

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
12 7959
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
"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
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
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
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
"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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: N Thorell | last post by:
I get 'file in use' exception when I call File.Delete("myFile.jpg"). But I can't see any reason for that. The file is open by any application. I have loaded the file into an PictureBox.Image...
2
by: John Smith Jr. | last post by:
I am trying to do a File.Delete(filename) from ASP.NET and I keep getting an error access denied. I have granted Full Control over the entire directory the file is in to ASP machine account.
6
by: I am Sam | last post by:
I keep getting this error and I don't know why: The path is too long after being fully qualified. Make sure path is less than 260 characters. Description: An unhandled exception occurred...
1
by: Chris | last post by:
We had an application running on a server that had been working for months with no problem. Last night, without any known change to the server, it started to fail. We tried all kinds of...
2
by: Raymond Du | last post by:
Hi, I am working on a VB.Net window application, the application use System.IO.File.Delete method to delete files, can those deleted files be restored? TIA
8
by: DavidB | last post by:
I'm trying to delete a file with File.Delete("c:\Documents and Settings\%username%\Application Data\Microsoft\Excel\excel*.xlb") On doing this I get an Exception "Illegal characters in path" -...
0
by: ABC | last post by:
Which function can determine the URI is web-based or File-based filename string? I want a function can determome the file with path is web or file-system-based filename, for example: ...
6
by: ~john | last post by:
I have binary files stored in a SQL Server table that I want the user to be able to download from an aspx webpage. The files are displayed on the form as "LinkButtons". When the link is clicked the...
3
by: srinivasanr | last post by:
I want to roll back file delete operaion using c#. How can i do it? I am deleting file using this code, File.Delete(fileName);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.