Why is access to the path denied?

前端 未结 29 1090
刺人心
刺人心 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:41

    I have found that this error can occur in DESIGN MODE as opposed to ? execution mode... If you are doing something such as creating a class member which requires access to an .INI or .HTM file (configuration file, help file) you might want to NOT initialize the item in the declaration, but initialize it later in FORM_Load() etc... When you DO initialize... Use a guard IF statement:

        /// <summary>FORM: BasicApp - Load</summary>
        private void BasicApp_Load(object sender, EventArgs e)
        {
            // Setup Main Form Caption with App Name and Config Control Info
            if (!DesignMode)
            {
                m_Globals = new Globals();
                Text = TGG.GetApplicationConfigInfo();
            }
        }
    

    This will keep the MSVS Designer from trying to create an INI or HTM file when you are in design mode.

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

    For those trying to make a UWP (Universal Windows) application, file permissions are much more restricted, and in general is deny by default. It also supersedes the system user permissions. You will basically only have access to files in either

    • Your install location
    • Your AppData location
    • Files selected through the File or Folder picker
    • Locations requested in your App Manifest

    You can read more here for details => https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions

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

    This is an old issue, but I ran into it while searching. Turns out that I was missing the actual filename component in the save path for SaveAs...

    string uploadPath = Server.MapPath("~/uploads");
    file.SaveAs(uploadPath); // BAD
    file.SaveAs(Path.Combine(uploadPath, file.FileName)); // GOOD
    
    0 讨论(0)
  • 2020-11-22 15:44

    I also had the problem, hence me stumbling on this post. I added the following line of code before and after a Copy / Delete.

    Delete

    File.SetAttributes(file, FileAttributes.Normal);
    File.Delete(file);
    

    Copy

    File.Copy(file, dest, true);
    File.SetAttributes(dest, FileAttributes.Normal);
    
    0 讨论(0)
  • 2020-11-22 15:44

    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".

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

    In my case the problem was Norton. My in-house program doesn't have the proper digital signature and when it tried to delete a file it gave the UnauthorizedAccessException.

    If it give you a notification, you can handle it from there. In my case it didn't give a notification that I noticed. So here's how to keep Norton from blocking the program.

    1. Open Norton
    2. Click the down arrow
    3. Click History
    4. Find activity by program
    5. Click More Options
    6. Click Exclude Process
    0 讨论(0)
提交回复
热议问题