Rapid File.Delete(Filename)导致UnauthorizedAccessException [英] Rapid File.Delete(Filename) causes UnauthorizedAccessException

查看:64
本文介绍了Rapid File.Delete(Filename)导致UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的循环:


If(Directory.Exists(tempDirectory))然后

尝试

Dim Files()As String = Directory.GetFiles(tempDirectory)

''清除目录

将每个文件名作为字符串存储在文件中

文件。删除(文件名)

下一页

Catch ex As Exception

结束尝试

结束如果


第一个文件将删除正常,但第二个文件将引发

UnauthorizedAccessException。


为了解决这个问题,我添加了在File.Delete之后的一个Thread.Sleep(1000)。


任何人都注意到如果你删除的东西太快,.NET会提出一个

UnauthorizedAccessException?有没有办法在之前的

删除完成后继续?


-

Lucas Tam(RE **** ****@rogers.com)

请删除删除来自回复时的电子邮件地址。
http://成员。 ebay.com/aboutme/coolspot18/

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/

推荐答案

Lucas,
第一个文件将删除正常,但是第二个文件会引发一个
UnauthorizedAccessException。
我无法在本地驱动器上重现Windows XP SP2上的错误,可以给你更多关于你试图删除的tempDirectory的详细信息

来自exists(例如网络驱动器)。


任何人都注意到如果你删除的东西太快,.NET会引发一个
UnauthorizedAccessException?
正如我上面所述,这里没有问题...

有没有办法在之前的
删除完成后继续?
将Try Catch放入For Each循环中。

For Each Filename As String in Files
Try File.Delete(Filename)
Catch ex As Exception

Debug.WriteLine(ex," File.Delete")

结束尝试下一个


希望这有助于

Jay

" Lucas Tam" < RE ******** @ rogers.com>在消息中写道

新闻:Xn *************************** @ 140.99.99.130 ..我有一个非常简单的循环:

if(Directory.Exists(tempDirectory))然后
尝试
Dim Files()As String = Directory.GetFiles(tempDirectory)
' '清除目录
将每个文件名作为字符串存储在文件中
File.Delete(文件名)
下一步
抓住ex作为例外
结束尝试
结束如果

第一个文件将删除正常,但第二个文件将引发一个
UnauthorizedAccessException。

为了解决这个问题,我添加了一个Thread.Sleep(1000)在File.Delete之后。

任何人都注意到如果你删除的东西太快,.NET会引发一个
UnauthorizedAccessException?以前
删除完成后有没有办法继续?

- 卢卡斯谭(RE ******** @ rogers.com)请删除删除来自回复时的电子邮件地址。
http://成员。 ebay.com/aboutme/coolspot18/
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/



" Jay B. Harlow [MVP-Outlook]" < JA ************ @ msn.com>在新闻中写道:

#X**************@TK2MSFTNGP14.phx.gbl:
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in news:
#X**************@TK2MSFTNGP14.phx.gbl:
任何人都注意到如果你删除的东西太快,.NET会引发一个
UnauthorizedAccessException?
Anyone notice that if you delete stuff too quickly, .NET will raise a
UnauthorizedAccessException?


如上所述,这里没有问题......


As I stated above, no problems here...




嗯......我想我发现了问题。我正在使用的一个组件不是

发布文件。


但是,出于某种原因我可以通过Windows资源管理器删除该文件。但

相同的文件不会被VB.NET删除...

不同的删除方法
VB.NET ???


-

Lucas Tam(RE ******** @ rogers.com)

请删除删除来自回复时的电子邮件地址。
http://成员。 ebay.com/aboutme/coolspot18/


根据UnauthorizedAccessException类。我猜你没有删除

的权限。右键单击文件 - >属性 - >安全 - >设置

完全控制为了你自己。再试一次。


希望帮助


" Lucas Tam"写道:
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:
我有一个非常简单的循环:

如果(Directory.Exists(tempDirectory))那么
尝试
昏暗的文件()As String = Directory.GetFiles(tempDirectory)
''清除目录
为文件中的每个文件名作为字符串
File.Delete(文件名)
下一页
Catch ex As Exception
结束尝试
结束如果

第一个文件将删除正常,但第二个文件将引发
UnauthorizedAccessException。

任何人都注意到如果你删除的东西太快,.NET会引发一个
UnauthorizedAccessException ?以前
删除完成后有没有办法继续?

- 卢卡斯谭(RE ******** @ rogers.com)请删除删除来自回复时的电子邮件地址。
http://成员。 ebay.com/aboutme/coolspot18/
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/



这篇关于Rapid File.Delete(Filename)导致UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆