When trying to create folder in My Documents with 'Controlled folder access' ON CreateFolder throws FileNotFoundException. I need to check if my app can create folder in My Documents. How I can check that I have permission for it when 'Controlled folder access' ON.
I tried DirectoryInfo.GetAccessControl method for it, it shows that it allows. Is only way now for me to try create a folder and handle FileNotFoundException.
try
{
Directory.CreateDirectory(Path.Combine(MyDocuments, "Foo"));
}
catch (FileNotFoundException)
{
//Do something
}
The best way to deal with this is by checking a couple of things.
This is done by checking the local machine registry key:
EnableControlledFolderAccess
(1 is enabled 0 for disabled) - note that this can also be nothing!SubKeyNames
thenGetValueNames()
for the keyAllowedApplications
- (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 theFileNotFoundException
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.