Способ № 1 (Рабочий, почти всегда)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
const FILE_READ_DATA = $0001; FILE_WRITE_DATA = $0002; FILE_APPEND_DATA = $0004; FILE_READ_EA = $0008; FILE_WRITE_EA = $0010; FILE_EXECUTE = $0020; FILE_READ_ATTRIBUTES = $0080; FILE_WRITE_ATTRIBUTES = $0100; FILE_GENERIC_READ = (STANDARD_RIGHTS_READ or FILE_READ_DATA or FILE_READ_ATTRIBUTES or FILE_READ_EA or SYNCHRONIZE); FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE or FILE_WRITE_DATA or FILE_WRITE_ATTRIBUTES or FILE_WRITE_EA or FILE_APPEND_DATA or SYNCHRONIZE); FILE_GENERIC_EXECUTE = (STANDARD_RIGHTS_EXECUTE or FILE_READ_ATTRIBUTES or FILE_EXECUTE or SYNCHRONIZE); FILE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or $1FF; |
1 2 3 4 5 6 |
function TfMain.IsFileOKToread(AFilePath:String): Boolean; begin Result:=False; if CheckFileAccess(FilePathConst,FILE_READ_DATA) = FILE_READ_DATA then Result:=True; end; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
function CheckFileAccess(const FileName: string; const CheckedAccess: Cardinal): Cardinal; var Token: THandle; Status: LongBool; Access: Cardinal; SecDescSize: Cardinal; PrivSetSize: Cardinal; PrivSet: PRIVILEGE_SET; Mapping: GENERIC_MAPPING; SecDesc: PSECURITY_DESCRIPTOR; begin Result := 0; GetFileSecurity ( PChar(Filename), OWNER_SECURITY_INFORMATION or GROUP_SECURITY_INFORMATION or DACL_SECURITY_INFORMATION, nil, 0, SecDescSize ); SecDesc := GetMemory(SecDescSize); if GetFileSecurity ( PChar(Filename), OWNER_SECURITY_INFORMATION or GROUP_SECURITY_INFORMATION or DACL_SECURITY_INFORMATION, SecDesc, SecDescSize, SecDescSize ) then begin ImpersonateSelf(SecurityImpersonation); OpenThreadToken(GetCurrentThread, TOKEN_QUERY, False, Token); if Token <> 0 then begin Mapping.GenericRead := FILE_GENERIC_READ; Mapping.GenericWrite := FILE_GENERIC_WRITE; Mapping.GenericExecute := FILE_GENERIC_EXECUTE; Mapping.GenericAll := FILE_ALL_ACCESS; MapGenericMask(Access, Mapping); PrivSetSize := SizeOf(PrivSet); AccessCheck(SecDesc, Token, CheckedAccess, Mapping, PrivSet, PrivSetSize, Access, Status); CloseHandle(Token); if Status then Result := Access; end; end; FreeMem(SecDesc, SecDescSize); end; |
1 2 3 4 5 |
procedure TfMain.bTestClick(Sender: TObject); begin if IsFileOKToread(FilePathConst) then ShowMessage('Ok To Read'); end; |
Способ №2 (Не рабочий, если доступ к файлу закрыт)
Данная функция НЕ РАБОТАЕТ, так как нужно проверять атрибуты безопасности файла, насколько я понимаю.
Простая проверка возможности прочитать файл. //True – файл открыт для чтения. Иначе – False.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function IsFileOkToRead(const aFileName : String; var EMessage:string) : Boolean; var fs:TFileStream; begin Result:=false; if not TFile.Exists(aFileName) then Exit; // trying - to open... try F:=TFile.OpenRead(aFileName); try Result := true; finally F.Free; end; except on E:Exception do begin EMessage:=E.ClassName+' '+E.message; Result := false; end; end; end; |