Why is access to the path denied?

前端 未结 29 1066
刺人心
刺人心 2020-11-22 15:25

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

if (result == \"Success\")
{
     if (FileUpload.HasFile)
     {
         t         


        
相关标签:
29条回答
  • 2020-11-22 15:54

    First just check the path if the colon(:) character is missing or not after the drive letter. If colon is not missing then you can check if access/write permission is granted for that path. I had the same issue and i was only missing the colon, permission and everything else was fine.

    C:\folderpath
    

    will work fine but,

    C\folderpath .........(missing colon)
    

    will give you access denial error.

    0 讨论(0)
  • 2020-11-22 15:55

    To solve this problem, I follow the Scot Hanselman approach at Debugging System.UnauthorizedAccessException (often followed by: Access to the path is denied) article, the code with example is bellow:

    class Program
    {
        static void Main(string[] args)
        {
            var path = "c:\\temp\\notfound.txt";
            try
            {
                File.Delete(path);
            }
            catch (UnauthorizedAccessException)
            {
                FileAttributes attributes = File.GetAttributes(path);
                if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    attributes &= ~FileAttributes.ReadOnly;
                    File.SetAttributes(path, attributes);
                    File.Delete(path);
                }
                else
                {
                    throw;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:56

    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));
    
    0 讨论(0)
  • 2020-11-22 15:57

    I got the error because I didn't realize that the destination should be a file. I had a folder as the second parameter (which works in cmd). and I got Unhandled Exception: System.UnauthorizedAccessException: Access to the path is denied. because C# File.Move wants a file there, not just for the first parameter, but for the second too, and so if you put a directory as second parameter, it's trying to write a file like c:\crp when you have a directory called c:\crp.

    this would be incorrect File.Move(args[0],"c:\\crp");

    So, this would be correct File.Move(args[0],"c:\\crp\\a.a");

    The same goes for File.Copy

    0 讨论(0)
  • 2020-11-22 15:57

    I had this error thrown when I tried to rename a folder very rapidly after it had been either moved or created.

    A simple System.Threading.Thread.Sleep(500); solved it:

    void RenameFile(string from, string to)
    {
       try
       {   
          System.IO.File.Move(from, to)      
       }   
       catch 
       {  
           System.Threading.Thread.Sleep(500);      
           RenameFile(from, to);      
       }   
    }
    
    0 讨论(0)
  • 2020-11-22 15:58

    Right click on Visual studio and click Run as Administrator

    0 讨论(0)
提交回复
热议问题