I want to determine that a user has write permission for a specified file.
This file can be opened by another process to write, so I can't use
CreateFile/OpenFile to check it. How can I do this?
Thanks for Your answer!
At
> I want to determine that a user has write permission for a specified
> file.
I'd say it depends on the file system. You may want to google for "ntfs
file permissions" and delphi - but wrestling with ACLs (Access Control
Lists) is not an especially easy task.
That's why most applications simply try and come back with an error if it
doesn't work. As you mentioned, you *may* have the permissions, but a
different process has the file in exclusive use - there go your
permissions...
--
Ben
>I want to determine that a user has write permission for a specified file.
>This file can be opened by another process to write, so I can't use
>CreateFile/OpenFile to check it. How can I do this?
As Ben wrote, it is not sufficient to determine whether you have
access rights. It's simpler to just take an axe to the problem. Here's
some sample code using CreateFile and TFileStream:
var
FHandle: THandle;
FStream: TFileStream;
...
SetLastError(0);
FHandle := CreateFile('MyFile',
GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, 0, 0);
if FHandle = INVALID_HANDLE_VALUE then
ShowMessage(SysErrorMessage(GetLastError))
else CloseHandle(FHandle);
try
SetLastError(0);
FStream := TFileStream.Create('MyFile', fmOpenReadWrite);
FStream.Free;
except
on E: EFOpenError do
ShowMessage(E.Message + #13#10 +
SysErrorMessage(GetLastError));
end;
Good luck.
Kurt