How do I prevent folders like My Pictures from being recreated?


Another converse of How do I programmatically create folders like My Pictures if they were manually deleted? and Why do folders like "My Pictures" come back after I delete them? is How do I prevent folders like My Pictures from being recreated?

Starting in Windows 7, there is a group policy called Disable Known Folders which lets you specify a list of known folders which should be disabled.

If somebody tries to create a known folder programmatically, the call will fail with ERROR_ACCESS_DISABLED_BY_POLICY.

Note that this policy only blocks creation of the folder. If the folder already exists, then the policy has no effect. (You're locking the door after the horse has bolted.)

Comments (24)
  1. Medinoc says:

    I'm not sure what this setting being a group policy entails. Does it mean only an administrator can set it, and for the whole machine?

  2. David Crowell says:

    Medinoc,

    Group policy has machine and user level policies.  This is a user-level policy.

  3. Fleet Command says:

    That'd be great. I can't wait to get rid of "My Documents" folder, whose actual name is "Junk that I didn't create".

    These known folders are also crowding "This PC" folder in Windows 8.1. I'd look up group policy to get rid of them too.

  4. Nawak says:

    Does this mean I can't create a folder named "My Pictures" or that I can't create a folder with "known folder ID" set to "My Pictures"? Or Both?

    [This refers to the creation of known folders. It has no effect on any other folders which happen coincidentally to have the same name as a known folder. -Raymond]
  5. Azarien says:

    I used to avoid those My Something folders since Windows 95. But with Windows 7 and its Libraries I gave up this fighting against the system and began using them.

  6. benjamin says:

    Gotta second Fleet Command's comment for this: the fact that Win8.1 deprecates Libraries in favor of the user directory-hosted Pictures/Music/etc. in the Open/Save dialogs is a huge pain (and one you seemingly can't resolve without disabling them in the Registry).

    It's especially infuriating because File History is a legitimately good feature that depends on a feature that, for whatever reason, Win8.1 has chosen to de-emphasize.

  7. Anon says:

    Next week:

    "A commenter recently emailed, wanting to know why they are suddenly receiving an error when attempting to programmatically create My Documents…"

  8. Richard Russell says:

    If the policy "only blocks creation of the folder" why wasn't it called 'Disable Known Folder Creation'?  Calling it 'Disable Known Folders' is bound to make people think it does something it doesn't.

  9. Maurits says:

    A way to block creation of a folder that happens to have the same name as a known folder is to pre-emptively create a file that has the same name.

  10. @ Maurits says:

    What? You can have a folder and a file with the same name.

  11. @Richard says:

    Er, because you need to read the policy description? How lazy are we?!

  12. Maurits says:

    > You can have a folder and a file with the same name

    Really?

    >echo. > test

    >mkdir test

    A subdirectory or file test already exists.

  13. Joshua says:

    But if you poke around enough you can have Xx and XX. Good one to stick on a thumb drive to confuse people.

  14. @ Maurits says:

    Whoops I take that back. I tested it with the "Pictures" folder but completely forgot I had the folder redirected to a different volume. LOL.

  15. Scarlet Manuka says:

    @Maurits: That sounds like a recipe for endless support calls along the lines of "I'm trying to save a picture, but I can't get into the "My Pictures" folder…"

  16. Joshua says:

    [This refers to the creation of known folders. It has no effect on any other folders which happen coincidentally to have the same name as a known folder. -Raymond]

    Which means that somebody who handled any error from looking up a known folder path by assuming what the default would be would thoroughly wreck this. (They'll also end up with a mess if it runs on an OS from the future where the path changed.)

    Incidentally, the way to create & set path to user-level known folders (besides the API that would be disabled here) in Windows XP got published. Anyway who was silly enough to actually use this would probably find their code worked in Windows 7 (see roaming profiles for why this probably still works), but risks their code not working in any future version (support for Windows 9 and Windows XP will have no overlap). Assume default value is a *lot* safer. Bail out the task that required it is a better idea.

  17. Azarien says:

    @benjamin: What? 8.1 disables Libraries? I don't understand why they would do that. Another reason to stick with Win7 as long as possible.

  18. Neil says:

    Known folders are not something I've ever thought about deleting or creating, and I couldn't find any mention of directory creation in the known folder API, nor did a search for the words "create a known folder programmatically" enlighten me, so could someone please point me at the appropriate documentation?

  19. Anonymous Coward says:

    @Azarien: I had the opposite thought when upgrading to Win7.

    "What? 7 has Libraries? I don't understand why they would do that. Another reason to stick with WinVista as long as possible."

  20. Anon says:

    @Neil

    Please, PLEASE continue not thinking about it.

    I'd also like people to stop thinking about deleting them.

    There's no valid reason to do the latter, and the only real reason to do the former is because someone did the latter.

    Circular technical support.

  21. Klimax says:

    @Azarien

    Libraries are not disabled… (At least when upgrading from 8.0)

  22. Fleet Command says:

    The "access denied" issue that some of you guys point out is significant. The matter wanting to save the the file from Paint into My Picture is not serious in comparison to some of the items that programs create in My Documents folder. Here is a list:

    confidentialfiles.wordpress.com/…/my-documents-i-am-framed

  23. Anon says:

    @Fleet Command

    Some blogs are filled to the brim with nonsense and terrible advice… and misrepresentations of what dates on products mean.

  24. 640k says:

    Instead of disabling it, use windows' approach of replacing system dll files from the dllcache.

    –> Use a watchdog tool which listens to file system events and deletes the junk folders. Works very well.

Comments are closed.