The best way to deal with this is by checking a couple of things.
- Check whether Controlled folder access is enabled on the computer
- If it is enabled then check to see if your application has been exempted from the control.
This is done by checking the local machine registry key:
HKLM\SOFTWARE\Policies\Microsoft\Windows Defender\Windows Defender Exploit
Guard\Controlled Folder Access
- Read the value for
EnableControlledFolderAccess
(1 is enabled 0 for disabled) - note that this can also be nothing!
- Loop through the
SubKeyNames
then GetValueNames()
for the key AllowedApplications
- (returns a string array)
If Controlled Folder Access is enabled and your application name (Assembly.GetExecutingAssembly().ManifestModule.Name
) is not in the AllowedApplications list, then you'll get the FileNotFoundException
and won't be able to create the folder (the user will also get a virus warning!).
EDIT:
There is also another subkey under that hive, ProtectedFolders
which lists the default folders that Microsoft have decided need to be protected (and include folders such as My Documents, My Pictures etc) plus any others that the user (administrator) may add. You could also check this (string array) if Controlled Folder Access is enabled on the system.