简体   繁体   中英

Why do I get message “Access to the path 'bootmgr' is denied ” when trying to clear the contents of a directory

Using Windows 10 Home (20H2) on HP Spectre

I am trying to use the following code to clear the contents of a directory

public int clearDirectory(string path)
{
    DirectoryInfo targetDir = new DirectoryInfo(path);
    foreach (FileInfo file in targetDir.GetFiles())
    {
        file.Delete();
    }
    foreach (DirectoryInfo dir in targetDir.GetDirectories())
    {
        dir.Delete(true);
    }
    return 0;
}

The target directory is on a USB SanDisk which has in its root dir one directory (which has a number of subdirectories) which I created and the following SanDisk files

SanDiskMemoryZone_AppInstaller.apk
SanDiskMemoryZone_QuickStartGuide.pdf

I replaced the path to the USB drive with a path to a directory on my C drive and that worked fine.

How does the bootmgr get involved in this?

In Windows you can declare for each drive if it is bootable or not. The bootable partition in Windows is a hidden drive called system partition (and that's also the reason why no error was shown when you tried to delete the C:-drive)

Your SAN-drive seems to be using a file system which has a hidden folder with ACL only for the system account.

That's why you get this error. You have no access right to delete the file/folder.

One solution:

  • change your code st It only selects files without a leading dot, like with regex: ^\.

If this still doesn't fix your error then you should try use an elevated process (with adminr rights) to tackle this.

REMARK: before you delete everything from a usb stick, it would be easier to just reformat it then deleting every file. Maybe look into Diskpart for this.

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