Вопрос Занятость файлов

Хамик

Старожил
Как с помощью Inno узнать занят ли файл чем либо и вывести инфу чем именно занят файл?
 

Crachlow

Старожил
Exec(ExpandConstant('{sys}\taskkill.exe'), ' /F /IM pzlib.exe', '', SW_HIDE, ewWaitUntilTerminated, i);
 

Andreo Fadio

Ветеран
Как с помощью Inno узнать занят ли файл чем либо
ХЗ как по профессиональности написания, но можно так:
Код:
[Setup]
AppName=MyProg
AppVerName=MyProg
DefaultDirName={pf}\MyProg
OutputDir=.\

[Code]
const
OPEN_EXISTING = 3;
FILE_SHARE_WRITE = 2;
GENERIC_WRITE = $40000000;
INVALID_HANDLE_VALUE = 4294967295;

function CreateFile(lpFileName: string; dwDesiredAccess, dwShareMode,
  lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes: DWORD;
  hTemplateFile: THandle): THandle;
  external 'CreateFileW@kernel32.dll stdcall';
function CloseHandle(hObject: THandle): BOOL;
  external 'CloseHandle@kernel32.dll stdcall';

function FileStatus(const AFileName: string): Boolean;
var
FileHandle: THandle;
begin
  Result:= False;
  FileHandle:= CreateFile(AFileName, GENERIC_WRITE, FILE_SHARE_WRITE, 0,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if FileHandle <> INVALID_HANDLE_VALUE then
  try
    Result:= True;
  finally
    CloseHandle(FileHandle);
  end;
end;

function InitializeSetup(): Boolean;
begin
if FileExists(ExpandConstant('{src}\test.wav')) then
if FileStatus(ExpandConstant('{src}\test.wav')) then
  MsgBox('Файл свободен!', mbInformation, MB_OK)
else MsgBox('Файл занят в другой программе!', mbInformation, MB_OK);
Result:= False;
end;
 
Последнее редактирование модератором:

Хамик

Старожил
@Andreo Fadio, проверил на rtf и mp3 все время выдает файл свободен, хотя файлы были открыты другими программами.
 

LexBell

Борода
Супер модератор
Вот такой вариант есть, на Restools версии запустится гарантированно, в плане работоспособности - должно, вроде.
code_language.pascal:
[Setup]
AppName=MyProg
AppVerName=MyProg
DefaultDirName={pf}\MyProg
OutputDir=.\

[Code]
function FileStatus(const AFileName: string): Boolean;
begin
  Result:= True;
  with TFileStream.Create(AFileName,fmShareExclusive) do
  try
    free;
  except
    Result:= False;
  end;
end;

function InitializeSetup(): Boolean;
begin
if FileExists(ExpandConstant('{src}\test.wav')) then
if FileStatus(ExpandConstant('{src}\test.wav')) then
  MsgBox('Файл свободен!', mbInformation, MB_OK)
else MsgBox('Файл занят в другой программе!', mbInformation, MB_OK);
Result:= False;
end;
 

Nemko

Дилетант
Модератор
Хамик, вроде как, с mp3 работает поскольку файл занят пока работает программа, которая его открывает. А с rtf не работает, так как файл освобождается после открытия его в редакторе. В интернете есть пример поиска по процессам и сравнением хэндлов, но статья написана для Ассемблера.
 

Хамик

Старожил
@Nemko, ассемблер я не понимаю, может тогда кто-нибудь либу напишет, раз такая сложность.
 

Ele

Новичок
ISFilePlus.dll - Inno setup plugin

I created a lib with small and useful functions for files and folders. :good:
It contains a function called "ISFileInUse". So it checks the specific file to see if it is in use.

code_language.pascal:
 if ISFileInUse ("D: \TestFile\setup1.exe") then
    MsgBox ('file is in use', mbConfirmation, MB_OK);

List of all functions from ISFilePlus.dll

ISFilePlus.dll: Folders

code_language.pascal:
function ISDirEmpty (Dir: widestring): Boolean; external 'ISDirEmpty @ files: ISFilePlus.dll stdcall';
function ISDirWriteable (Dir: widestring): Boolean; external 'ISDirWriteable @ files: ISFilePlus.dll stdcall';
function ISExistSubDirs (Dir: widestring): Boolean; external 'ISExistSubDirs @ files: ISFilePlus.dll stdcall';
function ISDirSizeMB (Dir: widestring): cardinal; external 'ISDirSizeMB @ files: ISFilePlus.dll stdcall';
function ISDirSizeStr (Dir: widestring): widestring; external 'ISDirSizeStr @ files: ISFilePlus.dll stdcall';

function ISOpenFolder (ADir, ATitle: widestring): widestring; external 'ISOpenFolder @ files: ISFilePlus.dll stdcall';

function ISCopyDir (Dir, dest: widestring; CopyRoot, Show: Boolean): Boolean; external 'ISCopyDir @ files: ISFilePlus.dll stdcall';
function ISMoveDir (Dir, dest: widestring; MoveRoot, Show: Boolean): Boolean; external 'ISMoveDir @ files: ISFilePlus.dll stdcall';
function ISRenameDir (Dir, dest: widestring; Show: Boolean): Boolean; external 'ISRenameDir @ files: ISFilePlus.dll stdcall';
function ISDeleteDir (Dir: widestring; DeleteRoot, permanent, Show: Boolean): Boolean; external 'ISDeleteDir @ files: ISFilePlus.dll stdcall';

function ISDirCreationTime (ADir: widestring): widestring; external 'ISDirCreationTime @ files: ISFilePlus.dll stdcall';
function ISDirLastAccessedTime (ADir: widestring): widestring; external 'ISDirLastAccessedTime @ files: ISFilePlus.dll stdcall';
function ISDirLastWrittenTime (ADir: widestring): widestring; external 'ISDirLastWrittenTime @ files: ISFilePlus.dll stdcall';
procedure ISDirChangeTime (ADir, ATime: widestring; DoRoot: boolean); external 'ISDirChangeTime @ files: ISFilePlus.dll stdcall';

ISFilePlus.dll: FindFiles

code_language.pascal:
function ISFindFiles (APath, FileMask: widestring): longint; external 'ISFindFiles @ files: ISFilePlus.dll stdcall';
function ISFileCount (FindHandle: longint): integer; external 'ISFileCount @ files: ISFilePlus.dll stdcall';
function ISPickFile (FindHandle: longint; AIndex: integer): widestring; external 'ISPickFile @ files: ISFilePlus.dll stdcall';
procedure ISFindFree (FindHandle: longint); external 'ISFindFree @ files: ISFilePlus.dll stdcall';

ISFilePlus.dll: Files

code_language.pascal:
function ISGetFileAttrbs (AFileName: widestring): widestring; external 'ISGetFileAttrbs @ files: ISFilePlus.dll stdcall';
function ISSetFileAttrbs (AFileName, Attrbs: widestring): boolean; external 'ISSetFileAttrbs @ files: ISFilePlus.dll stdcall';
function ISCheckFileAttrbs (AFileName: widestring; Attrb: widestring): boolean; external 'ISCheckFileAttrbs @ files: ISFilePlus.dll stdcall';
function ISClearFileAttrbs (AFileName: widestring): boolean; external 'ISClearFileAttrbs @ files: ISFilePlus.dll stdcall';

function ISFileIsDir (AName: widestring): boolean; external 'ISFileIsDir @ files: ISFilePlus.dll stdcall';
function ISFileIsHidden (Afile: widestring): boolean; external 'ISFileIsHidden @ files: ISFilePlus.dll stdcall';
function ISFileInUse (AName: widestring): boolean; external 'ISFileInUse @ files: ISFilePlus.dll stdcall';

function ISFileSizeMB (aFilename: widestring): cardinal; external 'ISFileSizeMB @ files: ISFilePlus.dll stdcall';
function ISFileSizeStr (aFilename: widestring): widestring; external 'ISFileSizeStr @ files: ISFilePlus.dll stdcall';

function ISCopyFile (AInFile, ADest: widestring; Show: boolean): boolean; external 'ISCopyFile @ files: ISFilePlus.dll stdcall';
function ISMoveFile (AInFile, ADest: widestring; Show: boolean): boolean; external 'ISMoveFile @ files: ISFilePlus.dll stdcall';
function ISRenameFile (AInFile, ADest: widestring; Show: boolean): boolean; external 'ISRenameFile @ files: ISFilePlus.dll stdcall';
function ISDeleteFile (AInFile: widestring; permanent, Show: boolean): boolean; external 'ISDeleteFile @ files: ISFilePlus.dll stdcall';

function ISFileOpen1 (AHandle: HWND; ADir, ATitle: widestring): widestring; external 'ISFileOpen1 @ files: ISFilePlus.dll stdcall';
function ISFileOpen2 (AHandle: HWND; ADir, ATitle, AFilter: widestring): widestring; external 'ISFileOpen2 @ files: ISFilePlus.dll stdcall';
function ISFileSave1 (AHandle: HWND; ADir, AFile, ATitle: widestring): boolean; external 'ISFileSave1 @ files: ISFilePlus.dll stdcall';
function ISFileSave2 (AHandle: HWND; ADir, AFile, ATitle, AFilter: widestring): boolean; external 'ISFileSave2 @ files: ISFilePlus.dll stdcall';

function ISFileVersion (Afile: widestring): widestring; external 'ISFileVersion @ files: ISFilePlus.dll stdcall';
function ISFileCreationTime (Afile: widestring): widestring; external 'ISFileCreationTime @ files: ISFilePlus.dll stdcall';
function ISFileLastAccessedTime (Afile: widestring): widestring; external 'ISFileLastAccessedTime @ files: ISFilePlus.dll stdcall';
function ISFileLastWrittenTime (Afile: widestring): widestring; external 'ISFileLastWrittenTime @ files: ISFilePlus.dll stdcall';
procedure ISFileChangeTime (AFile, ATime: widestring); external 'ISFileChangeTime @ files: ISFilePlus.dll stdcall';
 

Вложения

Сверху