【发布时间】:2014-03-20 22:45:06
【问题描述】:
有没有人见过 IO.File 方法与附加的调试器一起工作但在正常运行时不工作的问题?
IO.File.Delete 在运行时给出此异常,但如果调试器通过 VS 附加(在管理员模式下运行)则不会。
“对路径 'C:\AppName\App_Data\BodyPart_7416da26-4b8f-4d08-9a4a-fd3a9bf02327' 的访问被拒绝。”
我已验证 IIS_IUSRS 对 \App_Data 目录具有完全权限。 BodyPart_* 是 ASP.Net 生成的文件名,不是子目录。
另一个人在 StackOverflow 上遇到了这个问题,但目前还没有修复。 (File.Delete() not working in run mode but only works in debug mode)
我的代码:
''' <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
【问题讨论】:
-
究竟是什么不起作用? 不起作用是什么意思?你有例外吗?如果是这样,在哪里和哪个?
-
您是否尝试过以管理员身份运行程序集可执行文件?您是否也检查了文件权限?
-
添加有关异常的详细信息(哎呀)。是的,我已经检查了权限,这意味着我让 IIS_IUSRS 完全控制了目录。
-
您授予了访问哪个目录的权限?
C:\AppName\App_Data\BodyPart_7416da26-4b8f-4d08-9a4a-fd3a9bf02327,还是父目录? -
BodyPart_*是asp.net生成的临时文件名。我允许访问 \App_Data
标签: asp.net vb.net asp.net-web-api