.NET 4 Baby Steps - Part XI: Special folders
→ This post is part of a series—see the rest of the parts in the series index.
Environment.SpecialFolder
If you are building an application that takes advantage of special folders in Windows (e.g., My Documents), you’ll be happy to know that .NET 4 expanded the number of supported special folders with 25 new options in the Environment.SpecialFolder enum:
- AdminTools
- CDBurning
- CommonAdminTools
- CommonDesktopDirectory
- CommonDocuments
- CommonMusic
- CommonOemLinks
- CommonPictures
- CommonProgramFilesX86
- CommonPrograms
- CommonStartMenu
- CommonStartup
- CommonTemplates
- CommonVideos
- Fonts
- LocalizedResources
- MyVideos
- NetworkShortcuts
- PrinterShortcuts
- ProgramFilesX86
- Resources
- SystemX86
- Templates
- UserProfile
- Windows
Usage:
Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos));
GetFolderPath Method
The GetFolderPath method has also been updated with a new overload, accepting a second parameter: SpecialFolderOption, which offers three behaviors:
None: Returns the path without verifying its existence. Useful for network paths to reduce lag.Create(default): Verifies the folder path. Returns an empty string if the folder doesn’t exist.DoNotVerify: Forces creation of the folder if it’s missing.
Super-fast network example:
Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos, Environment.SpecialFolderOption.None));