Hi,
If the user enables
Controlled Folder Access mode in Windows 10/11 to protect certain folders (Documents, Desktop, etc.) or even drives, my application cannot write anything to the protected folders.
As far as I understand, there is no specific API to request permission from the user to write files to a protected folder. However, using the Save As dialog should automatically grant such permission.
As I checked, Lazarus can save project files (*.pas module files) to a protected folder.
My application cannot save files despite the Save As dialog.
- If a new file name is selected, I see an error right on top of the Save As dialog (even before it closes).
- If the file already exists, the dialog closes, but an exception occurs in the file creation code.
procedure TForm1.Button1Click(Sender: TObject);
var
fn : WideString;
stream : TFileStream;
begin
if SaveDialog1.Execute then begin
fn := SaveDialog1.FileName;
stream := nil;
try
if not FileExists(fn) then stream := TFileStream.Create(fn, fmCreate)
else stream := TFileStream.Create(fn, fmOpenReadWrite or fmShareDenyWrite);
try
stream.WriteAnsiString('test');
except
ShowMessage('Write File Error');
end;
finally
if Assigned(stream) then stream.Free
else ShowMessage('Create Stream Error');
end;
end;
end;
I checked other apps - some apps can write files to the protected folder, some can't (one app was written on Qt/C++). I have no idea what's going on.