简体   繁体   中英

Delete file from server: Access to the path is denied

I call this function from with my code-behind:
DeleteFile(Server.MapPath("/") + "sitemap_index.xml")

Public Shared Function DeleteFile(ByVal filename As String) As Boolean
'deletes file from server
Dim bResult As Boolean = False
Try
    If File.Exists(filename) Then
        'delete file
        File.Delete(filename)
        bResult = True
    Else
        bResult = True
    End If
Catch ex As Exception

End Try
Return bResult
End Function

I then get the error: Access to the path 'E:\zz\wwwroot\sitemap_index.xml' is denied.

In other sites of myself this logic works great, however on the current site it doesnt. I checked the security settings on my windows server 2008 R2 Standard.

See here the settings I have on my Windows server on folder wwwroot:

SYSTEM: Full Control
.NETWORK SERVICE: Read + Write + Read & Execute + List folder contents
IIS_IUSRS: Read + Write

As was suggested by other posts I've been reading I tried adding other user groups, but I have no AS.NET service/group on my server.

When logged in as administrator (Forms authentication) I can click a button to recreate the sitemap_index.xml and sitemaps.xml
Users should be able to delete and add images to the wwwroot\images\uploads folder

Which group should I give what permissions to allow the above to be possible AND secure?

Check the access for the Application Pool user.

Find the application pool that your site is using, right click on it and choose Advanced Settings... . The name of the user that the pool is using is listed next to Identity .

Note that if the identity says "ApplicationPoolIdentity", you should check the access for the user IIS AppPool\\<Name of the app pool here> Info about ApplicationPoolIdentity


It looks like Modify permissions are required to delete files . Try granting NetworkService Modify permissions.

I had this problem too. When I applied the codes in this way, I did not encounter any permission requests.

    public void DeleteDirectory(string target_dir)
    {
        string[] files = Directory.GetFiles(target_dir);
        string[] dirs = Directory.GetDirectories(target_dir);

        foreach (string file in files)
        {
            System.IO.File.SetAttributes(file, FileAttributes.Normal);
            System.IO.File.Delete(file);
        }

        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }

        Directory.Delete(target_dir, false);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM