[RESOLVED] [2005] Deleting File permission problem
Trying to use IO.File.Delete in order to delete a file from a website and it says I do not have permission to do so. I am using forms authentication to log in, and have a FileInfo custom web control that has a LoginView control inside of it to display a Delete linkbutton if the logged in user is a member of the "Admins" role. The control is working fine, however when I try to run the delete code in the click event, it gives me a permission error (access denied), and the file path is the correct absolute path. The page also has some upload code and the logged in users are able to upload files to the same directory fine. The file I am wanting to delete is in a subdirectory of where the page is located, and the current directory is set up to allow all logged in users (the 'Users' role and the 'Admins' role). Is there something I am missing? Should I delete the file in a different way?
code in the custom control with the delete code:
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.lblName.Text = _FileName
Me.lblSize.Text = _FileSize
Me.hlDownload.Text = _LinkText
Me.hlDownload.NavigateUrl = _FileURL
'find the link button in the loginview control, give it text so it is viewed
Dim lnkBtnDelete As LinkButton = _
DirectCast(Me.AdminView.FindControl("lnkbtnDelete"), LinkButton)
lnkBtnDelete.Text = "Delete"
'add the handler for the delete button click
AddHandler lnkBtnDelete.Click, AddressOf lnkBtnDelete_Click
End Sub
Protected Sub lnkBtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim FiletoDelete As String = Page.Request.PhysicalApplicationPath & "Members\Files\" & IO.Path.GetFileName(_FileURL)
IO.File.Delete(FiletoDelete)
End Sub
Re: [2005] Deleting File permission problem
Your not hosting your own site are you, using IIS?
Re: [2005] Deleting File permission problem
It appears like your own application is accessing the file, causing the lock. Where in your code is the file being used?
An ideal way to handle files that can be deleted or modified is to place them in a folder outside the application folder so that it doesn't get locked as part of the general process.
Re: [2005] Deleting File permission problem
The problem ended up being the permissions on the "Files" folder inside of the application. The Network Service account didn't have write/modify permissions on this folder (although if that was the case, how come I was able to upload files to that folder?). In any case, it is fixed now.
Re: [RESOLVED] [2005] Deleting File permission problem
This could also happen if you attempted a delete after upload. That's what I had suspected anyways, that a lock on the file had not been released after upload, which is why the denial occurred when attempting to delete.
Re: [RESOLVED] [2005] Deleting File permission problem
How would you go about releasing the lock if that was the case?
Re: [RESOLVED] [2005] Deleting File permission problem
Using FileStream.Close(), but that isn't the case with you, is it? You said that you can upload, but weren't able to delete. Somewhere in between, the process (aspnet) still has a lock on it. It could be other reasons that we haven't thought of yet
Re: [RESOLVED] [2005] Deleting File permission problem
Well it is a page with an upload option using a FileUpload control, using the SaveAs method to save the file. All users have this option, and after it uploads it refreshes the page with a control that shows the file information for each file in the directory. The "Admins" role would display a subsequent Delete LinkButton in a LoginView control (inside of the FileInfo control) that only displays for the "Admins" role, in order to delete the files they wish, all on the same page. The regular users would not get this option. Its just that the Delete button would give this message after debugging. After I modified the permissions, it was able to delete the files, but other weird things were happening, like the Delete button not showing until after a couple page postbacks, etc. I've decided to just have a general upload option and then in the Admin section, have a page listing the files with this delete option seperate from the normal upload page. Not sure if this was due to the LoginView control inside of the actual FileInfo control that displays for each file or not, it was just real buggy however I had it coded. I figured the new solution would be easier instead of trying to debug, rather unsuccessfully, why the weird things were happening :)