IO.File.Delete工作在调试模式,但会引发安全异常,在运行模式 [英] IO.File.Delete works in debug mode but throws security exception in run mode

查看:366
本文介绍了IO.File.Delete工作在调试模式,但会引发安全异常,在运行模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人见过哪里IO.File方法附加调试工作,但不是在正常运行时的问题?

IO.File.Delete给出了这样的例外在但如果调试器是通过VS(运行在管理员模式)连接的运行时间。

访问路径C:\\ AppName \\目录的App_Data \\ BodyPart_7416da26-4b8f-4d08-9a4a-fd3a9bf02327'。被拒绝

我已经验证IIS_IUSRS对\\ App_Data目录完全控制权限。 BodyPart_ *是ASP.Net生成的文件名,而不是一个子目录。

有StackOverflow上这个问题,但没有修复作为然而另外一个人。 (<一href=\"http://stackoverflow.com/questions/10068547/file-delete-not-working-in-run-mode-but-only-works-in-debug-mode\">File.Delete()在运行模式下不工作,但仅在调试模式工作)

我的code:

 '''&LT;总结&gt;
'''邮政文件(S)和通过VDocument WS储存
'''&LT; /总结&gt;
&LT; System.Web.Http.HttpPost&GT;
&LT; ActionNameAttribute(PostFiles)&GT; _
公共职能PostFiles(小于FromUri&GT; fileGroupGuid作为GUID)作为Htt的presponseMessage    昏暗newFileIds作为新的列表(整数)
    昏暗filesToDelete作为新的列表(串)    检查是否请求包含的multipart / form-data的。
    如果不Request.Content.IsMimeMultipartContent()然后
        抛出新的Htt presponseException(的HTTPStatus code.UnsupportedMediaType)
    万一
    暗淡根作为字符串= HttpContext.Current.Server.MapPath(〜/ App_Data文件)
    昏暗的提供商=新MultipartFormDataStreamProvider(根)    '读取表单数据。
    Request.Content.ReadAsMultipartAsync(供应商)    对于每个文件MultipartFileData在provider.FileData        商店VDOC服务器
        昏暗vdocService作为新wsdocument.vdocument
        昏暗vdocId作为字符串
        昏暗sOrigFileName作为字符串=/&放大器; file.Headers.ContentDisposition.FileName.Replace(,)        vdocId = vdocService.savedocument(IO.File.ReadAllBytes(file.LocalFileName),sOrigFileName,_
                              FS上传,0,0,0,0)        存储在数据库中发布文件引用
        昏暗FILEID作为整数=新建答案()。StoreAnswerFileWithVDocumentId(fileGroupGuid.ToString,sOrigFileName,0,file.Headers.ContentType.ToString,新的字节(-1){} 0,_
            0,FSFileMode.RespondentAnswer,Convert.ToInt32(vdocId))        newFileIds.Add(FILEID)        filesToDelete.Add(file.LocalFileName)    下一个    对于每一个临时文件作为字符串在filesToDelete
        删除临时文件
        IO.File.Delete(临时文件)
    下一个    返回Request.CreateResponse(的HTTPStatus code.Accepted,newFileIds)
结束功能


解决方案

在调试模式没有得到的异步调用'ReadAsMultipartAsync等线程被阻塞,直到方法完成创建一个新的线程。在发布模式,它使用了异步调用一个新的线程,因为该方法在一个单独的线程中运行,你的code的其余部分在当前线程上仍在处理。当你到删除的文件,这些文件仍然在附加线程的ReadAsMultipartAsync的方法锁定。由于文件仍然锁定,删除将失败。你需要等待'ReadAsMultipartAsync,因此,它完成在继续处理之前。

试试这个:

 等待Request.Content.ReadAsMultipartAsync(供应商)

Has anyone ever seen an issue where IO.File methods work with the debugger attached but not at normal run-time?

IO.File.Delete gives this exception at run-time but not if the debugger is attached via VS (which runs in admin mode).

"Access to the path 'C:\AppName\App_Data\BodyPart_7416da26-4b8f-4d08-9a4a-fd3a9bf02327' is denied."

I have verified IIS_IUSRS has full permissions on the \App_Data directory. BodyPart_* is a file name generated by ASP.Net, not a sub-directory.

One other person having this issue on StackOverflow but no fixes as of yet. (File.Delete() not working in run mode but only works in debug mode)

My code:

''' <summary>
'''Post file(s) and store it via VDocument WS
''' </summary>
<System.Web.Http.HttpPost>
<ActionNameAttribute("PostFiles")> _
Public Function PostFiles(<FromUri> fileGroupGuid As Guid) As HttpResponseMessage

    Dim newFileIds As New List(Of Integer)
    Dim filesToDelete As New List(Of String)

    ' Check if the request contains multipart/form-data.
    If Not Request.Content.IsMimeMultipartContent() Then
        Throw New HttpResponseException(HttpStatusCode.UnsupportedMediaType)
    End If


    Dim root As String = HttpContext.Current.Server.MapPath("~/App_Data")
    Dim provider = New MultipartFormDataStreamProvider(root)

    ' Read the form data.
    Request.Content.ReadAsMultipartAsync(provider)

    For Each file As MultipartFileData In provider.FileData

        'Store to VDoc Server
        Dim vdocService As New wsdocument.vdocument
        Dim vdocId As String
        Dim sOrigFileName As String = "/" & file.Headers.ContentDisposition.FileName.Replace("""", "")

        vdocId = vdocService.savedocument(IO.File.ReadAllBytes(file.LocalFileName), sOrigFileName, _
                              "FS Upload", "0", "0", "0", "0")

        ' Store the posted file reference in the database
        Dim fileId As Integer = New Answer().StoreAnswerFileWithVDocumentId(fileGroupGuid.ToString, sOrigFileName, 0, file.Headers.ContentType.ToString, New Byte(-1) {}, 0, _
            0, FSFileMode.RespondentAnswer, Convert.ToInt32(vdocId))

        newFileIds.Add(fileId)

        filesToDelete.Add(file.LocalFileName)

    Next

    For Each tempFile As String In filesToDelete
        'delete the temp file
        IO.File.Delete(tempFile)
    Next

    Return Request.CreateResponse(HttpStatusCode.Accepted, newFileIds)


End Function

解决方案

In debug mode a new thread is not getting created for the async call 'ReadAsMultipartAsync' and so the thread is blocked until that method completes. In release mode, it's using a new thread for the async call and since that method is running on a seperate thread, the rest of your code is still processing on the current thread. When you get to deleting the files, the files are still locked by the 'ReadAsMultipartAsync' method on the additional thread. Since the files are still locked, the delete will fail. You need to await the 'ReadAsMultipartAsync', so that it completes before you continue processing.

Try this:

await Request.Content.ReadAsMultipartAsync(provider)

这篇关于IO.File.Delete工作在调试模式,但会引发安全异常,在运行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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