Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My application involves in deleting a file and moving the newly created file to the deleted file path. It is properly working when i am running it locally, I hosted that application in my local iis but it is throwing me an error when it comes to the delete functionality of the file.

Error Message:
Access to the path 'File Path' is denied.

What I have tried:

1) I have added IIS_IUSRS identity with FullControl access for the file security property.
2) Enabled the Directory Browsing in my local iis server.

but no luck. I am getting the same error. Please suggest me something. Thanks in advance.
Posted
Updated 4-May-17 5:08am
v2
Comments
CHill60 4-May-17 10:02am    
Share the code that you are using
Group 13172486 4-May-17 10:41am    
Private Function GenerateScripts(_latestFXRates As RateExtract) As Boolean
Dim fileName As String = Nothing
Dim updatedScriptFilePath As String
Dim FileInfo As IO.FileInfo
Dim FileAcl As New FileSecurity

'Try
Dim fileEntries As String() = Directory.GetFiles(tfsDirectoryName)
' Process the list of files found in the directory.
For Each fileName In fileEntries

FileInfo = New IO.FileInfo(fileName)

If FileInfo.IsReadOnly Then
FileAcl.AddAccessRule(New FileSystemAccessRule("IIS_IUSRS", FileSystemRights.FullControl, AccessControlType.Allow))
FileInfo.SetAccessControl(FileAcl)
End If
If fileName.Contains("Step1_Backup_all_tables.sql") Then

updatedScriptFilePath = UpdateBackUpAllTables(fileName)
MoveTheChangesToTFS(fileName, updatedScriptFilePath)

End If
End Function

Private Sub MoveTheChangesToTFS(fileName As String, updatedScriptFilePath As String)

File.Delete(fileName) --> Getting an error at this point
File.Move(updatedScriptFilePath, fileName)

End Sub


CHill60 4-May-17 10:43am    
See solution 2 - what is the value of updatedScriptFilePath
Group 13172486 4-May-17 10:52am    
it holds the path of the file from where it has to get the new file
CHill60 4-May-17 11:12am    
I'd guessed that much. My question was "what is the value of ..."

It would appear as though you're granting permissions to the files themselves when you should be granting permissions to the folder the files are in.

As has been said before, you should be creating a user account to run the app pool your site is hosted in. This ID should be granted permissions to do whatever the code needs to the folder containing the files you're working with.

These permissions should NOT be granted in your website code. They should be setup when the application is deployed to the server.
 
Share this answer
 
Start by using the debugger - or logging if you only get this in production - to find out exactly what the path is, and then look at what needs permissions.

But I'm guessing that you are trying to access the client hardware and delete / move a file onto it. That won't work - it seems to in development because the client and the server are both the same computer, but in production they aren't, and the server (which is where your VB code is run) has no access at all to client hardware, so you are getting the error message.
Finding out the actual path is the important bit to start with though.
 
Share this answer
 
Comments
Group 13172486 9-May-17 0:54am    
I have changed the location from C:/ to other partition but still i am end up with the same error. I am trying to delete a file in TFS source control explorer which is working perfectly alright locally only when it is through IIS i am getting this access denied error. Do you have any idea do we need to have permission for any specific users or groups as we have IIS_IUSRS for Application WebSite in IIS?
I would suggest you change the identity of the application pool in IIS to use a local account that you grant sufficient permissions to, not one that is built in like you already did. Just create a new account and give it permissions.

Also make sure you are not using the Impersonate tag in Web.Config.
 
Share this answer
 
Comments
Group 13172486 4-May-17 10:45am    
Thanks for the suggestion...I will give a try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900