Why is access to the path denied?

2019-01-01 02:16发布

I am having a problem where I am trying to delete my file but I get an exception.

if (result == "Success")
{
     if (FileUpload.HasFile)
     {
         try
         {
              File.Delete(Request.PhysicalApplicationPath + app_settings.login_images + txtUploadStatus.Text);
              string filename = Path.GetFileName(btnFileUpload.FileName);
              btnFileUpload.SaveAs(Request.PhysicalApplicationPath + app_settings.login_images + filename);
         }
         catch (Exception ex)
         {
               Message(ex.ToString());
         }
      }
}

Also I should note that the folder I am trying to delete from has full control to network services.

The full exception message is:

System.UnauthorizedAccessException: Access to the path 'C:\Users\gowdyn\Documents\Visual Studio 2008\Projects\hybrid\hybrid\temp_loginimages\enviromental.jpg' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at hybrid.User_Controls.Imgloader_Add_Edit_Tbl.btnUpdate_Click(Object sender, EventArgs e) in C:\Users\gowdyn\Documents\Visual Studio 2008\Projects\hybrid\hybrid\User_Controls\Imgloader_Add_Edit_Tbl.ascx.cs:line 242

Any ideas?

23条回答
美炸的是我
2楼-- · 2019-01-01 02:47

The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error.

I hit the same thing. Check to ensure that the file is NOT HIDDEN.

查看更多
查无此人
3楼-- · 2019-01-01 02:48

You need to modify the privileges of the folder you're trying to delete from/save to. Right-click on the containing folder and use the Security tab to permit modify rights for the user your application runs under.

查看更多
人气声优
4楼-- · 2019-01-01 02:48

I was facing this error because

Sometimes when I Combine the path with File Name and FileName = ""

It become Path Directory not a file which is a problem as mentioned above

so you must check for FileName like this

if(itemUri!="")
        File.Delete(Path.Combine(RemoteDirectoryPath, itemUri));
查看更多
零度萤火
5楼-- · 2019-01-01 02:48

In my particular case I was repeatedly creating and deleting 10000 folders. It seems to me that the problem was in that although the method Directory.Delete(path, true) returns, the underling OS mechanism may still be deleting the files from the disk. And when I am starting to create new folders immediately after deletion of old ones, some of them are still locked because they are not completely deleted yet. And I am getting System.UnauthorizedAccessException: "Access to the path is denied".

enter image description here

Using Thread.Sleep(5000) after Directory.Delete(path, true) solves that problem. I absolutely agree that this is not safe, and I am not encouraging anyone to use it. I would love to here a better approach to solve this problem to improve my answer. Now I am just giving an idea why this exception may happen.

class Program
{
    private static int numFolders = 10000;
    private static string rootDirectory = "C:\\1";

    static void Main(string[] args)
    {
        if (Directory.Exists(rootDirectory))
        {
            Directory.Delete(rootDirectory, true);
            Thread.Sleep(5000);
        }

        Stopwatch sw = Stopwatch.StartNew();
        CreateFolder();
        long time = sw.ElapsedMilliseconds;

        Console.WriteLine(time);
        Console.ReadLine();
    }

    private static void CreateFolder()
    {
        var one = Directory.CreateDirectory(rootDirectory);

        for (int i = 1; i <= numFolders; i++)
        {
            one.CreateSubdirectory(i.ToString());
        }
    }
}
查看更多
琉璃瓶的回忆
6楼-- · 2019-01-01 02:51

Be aware that if you are trying to reach a shared folder path from your code, you dont only need to give the proper permissions to the physicial folder thru the security tab. You also need to "share" the folder with the corresponding app pool user thru the Share Tab

查看更多
春风洒进眼中
7楼-- · 2019-01-01 02:53

According to File.Delete Method...

An UnauthorizedAccessException means one of 4 things:

  • The caller does not have the required permission.
  • The file is an executable file that is in use.
  • Path is a directory.
  • Path specified a read-only file.
查看更多
登录 后发表回答