Results 1 to 10 of 10

Thread: [RESOLVED] Access Denied on File Delete

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Resolved [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?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,633

    Re: Access Denied on File Delete

    Yeah, just give the "users" account read,write,modify. Should work.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Access Denied on File Delete

    Quote Originally Posted by sapator View Post
    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).
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,633

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,633

    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)
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Access Denied on File Delete

    Quote Originally Posted by sapator View Post
    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?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  10. #10
    New Member
    Join Date
    May 2024
    Posts
    2

    Re: Access Denied on File Delete

    Quote Originally Posted by SeanGrebey View Post
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width