I’m working on an app that needs to write files to user-selected folders. The problem is when Controlled Folder Access (CFA) is on. If a user tries to save in a CFA-protected folder, like Documents, it’s blocked. The error message is confusing, saying ‘file not found’ and showing a system tray notification about CFA blocking the app.
I can check for CFA at startup and give a clear message. But I can’t do this in SaveFileDialog. After the ‘file not found’ error, the dialog closes without info on where the user tried to save.
How can I give a helpful message when SaveFileDialog hits a CFA folder? I don’t want to force users to allow the app by default, as not everyone uses CFA. Any ideas on how to handle this without confusing users?
Hey there! This is a tricky situation, isn’t it?
I’ve been wondering about something similar myself.
Have you considered trying to guide users towards safer locations by default? Maybe you could set the initial directory of the SaveFileDialog to a folder that’s less likely to be protected by CFA, like the user’s Downloads folder?
Something like:
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\Downloads";
This way, you’re subtly steering users away from potential CFA issues without explicitly restricting their choices. Plus, it might save them a few clicks!
I’m curious though - have you noticed any patterns in terms of which folders tend to cause problems most often? It might be helpful to compile a list of ‘high-risk’ locations to watch out for.
Oh, and here’s a wild thought - what if you implemented a simple ‘test save’ feature that users could run voluntarily? It could check a bunch of common locations and report back on which ones are safe to use. Might be overkill, but could be a fun addition for power users!
What do you think? Have you tried anything like this already?
yo, that’s a tough one. what if u try catching the specific exception when saving? like, wrap ur save code in a try-catch and look for that ‘UnauthorizedAccessException’. then u could pop up a message saying ‘hey, looks like CFA’s blockin this. try a different spot or add the app to ur allowed list’. might help users figure out whats goin on
I’ve faced a similar issue with Controlled Folder Access in one of my projects. Unfortunately, there’s no direct way to detect CFA restrictions within SaveFileDialog. However, here’s a workaround I’ve found effective:
After the user selects a location in SaveFileDialog, but before actually writing the file, perform a test write to that location using a temporary file. If this fails due to CFA, you can catch the specific exception and provide a clear message about the restriction.
Something like:
try {
string tempFile = Path.Combine(selectedPath, "test.tmp");
File.WriteAllText(tempFile, "test");
File.Delete(tempFile);
} catch (UnauthorizedAccessException) {
MessageBox.Show("Controlled Folder Access is preventing the save. Please choose a different location or add this app to the allowed list in Windows Security settings.");
return;
}
This approach allows you to give users specific guidance without relying on the limited error handling in SaveFileDialog itself.