Определяем доступность файла на чтение и запись.
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('Файл доступен для чтения') else ShowMessage('Файл не доступен для чтения'); if isFileFree(filePath, fmOpenWrite) = true then ShowMessage('Файл доступен для записи') else ShowMessage('Файл не доступен для записи'); end; |
Параметры
filePath - путь к проверяемому файлу
Результат
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 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; |