Page 1 of 1

Determining if I can access certain file

Posted: Mon Nov 07, 2005 7:47 pm
by smithmarkduane
Hi:

Is it possible to use madSecurity to determine if the current user can create or write to a specific file.

For example, if the app needs to update a file at:

\\server\someshare\somefile.txt

Can I determine access before trying to update the file?

Thanks,
Mark

Posted: Tue Nov 08, 2005 7:43 pm
by madshi
Well, theoretically it's possible. I mean you can list all the elements of the ACL. However, you'd need quite a complicated logic to try and find out what the ACL sais about your user. So I don't recommend this approach. The most simple way is to try to open the file. If it succeeds then you had enough rights.

Posted: Tue Nov 08, 2005 7:51 pm
by smithmarkduane
Thanks for the reply. I came to the same conclusion shortly after I posted this message. I am now using successfully using:

function IsFileAccessible(fName: string): boolean;
var
HFileRes: HFILE;
begin
Result := false;
HFileRes := CreateFile(
pchar(fName),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0);

Result := (HFileRes <> INVALID_HANDLE_VALUE);
if Result then CloseHandle(HFileRes);
end;