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
FileNotFoundExceptionis certainly unexpected here asDirectory.CreateDirectorydoes not have it in the list of documented exceptions. This exception seems to be the result of Windows Defender blocking your app from accessing the folder and not the Windows File System which is on the lower level and hence theSystem.IOis giving you a successful result.Check this thread as it seems to be your situation. And the very last comment is suggesting to allow the app via Controlled access list. I suspect until the proper exception type is implemented low level you are pretty much stuck catching the undocumented exception here. There might be some undocumented WinAPI to ask the Windows Defender if your app has access to the folder, but relying on it might be more error prone in the long run. Hope it helps!