[RESOLVED] Access Denied on File Delete
I have an application that allows users to use a FileUpload object to upload files to a directory on the web server. The upload part works fine, but if they later try to delete the file they get an error that Access to the path is denied. The Application Pool for the site runs under NetworkService, and on the folder they are loading and trying to delete these files from I give NetworkService Full control. Anyt thoughts as to what else I may need to do?
Re: Access Denied on File Delete
Yeah, just give the "users" account read,write,modify. Should work.
Re: Access Denied on File Delete
Quote:
Originally Posted by
sapator
Yeah, just give the "users" account read,write,modify. Should work.
It already has full control. Something weird is going on, and I'm not sure what. I'm wondering if it has something to do with the folder it is in. I didn't really know IIS 7 when I loaded this guy (can't say I know it much better now, so I just dropped the website where the default page was setup already which was a weird place under inetpub (htdocs folder).
Re: Access Denied on File Delete
Create you website, after that create a new folder where the to del - files will be in, now insert an new aspx page inside the file delete folder, a blank default.aspx will do.Give the appropriate permissions. Let me know.
If it does not work then it's your code. Just to be on the safe side, write here the exact permissions you give to this folder and the exact code you are using for deleting the files.
Re: Access Denied on File Delete
Code:
Protected Sub gvFiles_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvFiles.RowCommand
' Finish Me
' Restrict users to Stephen/Martin
If e.CommandName = "Delete" Then
Dim oSQL As New clsSQL
Dim strFileName As String = ""
oSQL.GetFileName(strFileName, gvFiles.DataKeys(e.CommandArgument).Value())
Dim strFilePath As String = Server.MapPath("~\ACSDocs\" & strFileName)
System.IO.File.Delete(strFilePath)
oSQL.DeleteACSDocs(gvFiles.DataKeys(e.CommandArgument).Value())
oSQL.LoadACSDocs()
gvFiles.DataSource = oSQL.Dataset.Tables("ACSDocs")
gvFiles.DataBind()
End If
End Sub
Permission wise I am giving Network Service (app pool runs under) Full Control. I've tried Users, Administrators, System. Changed the App Pool to the IIS User Account and gave it full control. It's bizarre. Before we moved to a VPS and were on a shared server they could delete, so I don't think it is a code issue.
Re: Access Denied on File Delete
Can you try deleting outside a gridview? I am not sure how well the server is behaving with in grid delete.
Also try changing to this: Server.MapPath("\\ACSDocs\ ..... etc)
Re: Access Denied on File Delete
Quote:
Originally Posted by
sapator
Can you try deleting outside a gridview? I am not sure how well the server is behaving with in grid delete.
Also try changing to this: Server.MapPath("\\ACSDocs\ ..... etc)
Strangely this may be my issue and I don't know why.
I tried moving where the documents are to just C:\Documents\ACSDocs\ on the server, gave it rights to Network Service, dropped a button on it and a text dox and have one line:
Code:
System.IO.File.Delete(txtTestDelete.Text)
Deletes the file no problem. So I thought I had a fix, changed the code to point to that directory:
Code:
If e.CommandName = "Delete" Then
Dim oSQL As New clsSQL
Dim strFileName As String = ""
oSQL.GetFileName(strFileName, gvFiles.DataKeys(e.CommandArgument).Value())
'Dim strFilePath As String = Server.MapPath("~\ACSDocs\" & strFileName)
Dim strFilePath As String = "C:\Documents\ACSDocs\" & strFileName
System.IO.File.Delete(strFilePath)
oSQL.DeleteACSDocs(gvFiles.DataKeys(e.CommandArgument).Value())
oSQL.LoadACSDocs()
gvFiles.DataSource = oSQL.Dataset.Tables("ACSDocs")
gvFiles.DataBind()
End If
Run the application again, get the same Access Denied error. Both my Test.aspx page and my real .aspx page live in the same directory. I'm a bit at a loss. You think the GridView somehow is screwing it up?
Re: Access Denied on File Delete
Huh almost sure it is the gridview now. Thought maybe it was the built in delete, so did a button instead and named the command "deletefile" but that didn't resolve it. So I tried dropping the one liner from my test page that worked on the page outside the gridview and sure enough it can delete from the directory no problem.
So...any advice on what in the RowCommand would cause it I am open to.
Re: Access Denied on File Delete
SOB ok I figured it out. I feel pretty stupid now.
This issue is with this line:
Code:
oSQL.GetFileName(strFileName, gvFiles.DataKeys(e.CommandArgument).Value())
returning an empty string, so instead of trying to delete a file, I was trying to delete a directory and that was failing.
Thanks for the help.
Re: Access Denied on File Delete
Quote:
Originally Posted by
SeanGrebey
SOB ok I figured it out. I feel pretty stupid now.
This issue is with this line:
Code:
oSQL.GetFileName(strFileName, gvFiles.DataKeys(e.CommandArgument).Value())
returning an empty string, so instead of trying to delete a file, I was trying to delete a directory and that was failing.
Thanks for the help.
Have you tried Longpath tool?