在另一个进程中打开文件时,Inno Setup LoadStringFromFile失败 [英] Inno Setup LoadStringFromFile fails when file is open in another process
问题描述
要检查何时启动数据库(SQL Anywhere)并准备接收请求,我将数据库消息窗口输出到日志(文本)文件,然后尝试使用LoadStringFromFile
读取它,然后使用Pos
搜索特定文本.问题在于,由于文件正在使用中,因此失败(我认为).
To check to see when a database (SQL Anywhere) is fired up and ready to receive requests I am outputting the database message window to a log (text) file and then attempting to read this using LoadStringFromFile
, which I then search for specific text using Pos
. The problem is that this fails (I assume) as the file is in use.
Exec(strInstallPath + '\Bin32\dbeng17.exe', '-n ' + strEngineName + ' "' + strInstallPath + '\Database\Olympus.db" -n ' + strDatabaseName + ' -gdall -xtcpip -ti0 -c25p -ot "' + strTempPath + '\dbeng.log"', '', SW_HIDE,
ewNoWait, intResultCode);
if not LoadStringFromFile(strTempPath + '\dbeng.log', astrDatabaseEngineLog) then
begin
Log('Loading string from file failed.');
end;
我还尝试过使用FileCopy
复制日志文件,并尝试从该文件的副本中读取,但是FileCopy
也会失败.
I have also tried to copy the log file using FileCopy
and attempt to read from the copy of the file, but FileCopy
also fails.
if not FileCopy(strTempPath + '\dbeng.log', strTempPath + '\dbengcopy.log', False) then
begin
Log('File copy failed.');
end;
是否有任何方法可以读取正在使用的文件或执行此操作的另一种方法?
Is there any way to read from a file that is in use or another way to do this?
推荐答案
使用TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone)
.
在Inno Setup的Unicode版本(Inno Setup 6中唯一的版本)中,由于类的接口错误,其使用非常棘手.
In Unicode version of Inno Setup (the only version as of Inno Setup 6), its use is tricky due to a bad interface of the class.
function BufferToAnsi(const Buffer: string): AnsiString;
var
W: Word;
I: Integer;
begin
SetLength(Result, Length(Buffer) * 2);
for I := 1 to Length(Buffer) do
begin
W := Ord(Buffer[I]);
Result[(I * 2)] := Chr(W shr 8); { high byte }
Result[(I * 2) - 1] := Chr(Byte(W)); { low byte }
end;
end;
function LoadStringFromLockedFile(const FileName: string; var S: AnsiString): Boolean;
var
Buffer: string;
Stream: TFileStream;
begin
Result := True;
try
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
SetLength(Buffer, Stream.Size div 2);
Stream.ReadBuffer(Buffer, Stream.Size);
S := BufferToAnsi(Buffer);
finally
Stream.Free;
end;
except
Result := False;
end;
end;
代码基于
The code is based on TLama's code posted in Read bytes from file at desired position with Inno Setup.
这篇关于在另一个进程中打开文件时,Inno Setup LoadStringFromFile失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!