Determine the file's read and write accessibility.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var filePath: WideString; const fmOpenRead = $0000; fmOpenWrite = $0001; begin filePath:= 'C:\Windows\explorer.exe'; if isFileFree(filePath, fmOpenRead) = true then ShowMessage('The file is readable') else ShowMessage('The file is not readable'); if isFileFree(filePath, fmOpenWrite) = true then ShowMessage('File is writable') else ShowMessage('The file is not writable'); end; |
Options
filePath - path to the scanned file
Result
True - if access to the file meets the required level, False - if not or the file does not exist.
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 |
function isFileFree(filePath: WideString; accessType: Word): boolean; var f: TFileStream; begin if accessType = fmOpenRead then begin try f := TFileStream.Create(filePath, accessType or fmShareDenyNone); try Result := true; finally f.Free; end; except Result := false; end; end; if accessType = fmOpenWrite then begin try f := TFileStream.Create(filePath, accessType or fmShareExclusive); try Result := true; finally f.Free; end; except Result := false; end; end; end; |