有没有办法检查Delphi中的ReWrite是否已打开文件?
代码将是这样的:
AssignFile(textfile, 'somefile.txt'); if not textFile.IsOpen then Rewrite(textFile);
Toon Krijthe.. 17
你可以得到文件模式.(有一刻,我会创造一个例子).
TTextRec(txt).Mode为您提供模式:
55216 = closed 55217 = open read 55218 = open write fmClosed = $D7B0; fmInput = $D7B1; fmOutput = $D7B2; fmInOut = $D7B3;
在系统单元中搜索TTextRec以获取更多信息.
你可以得到文件模式.(有一刻,我会创造一个例子).
TTextRec(txt).Mode为您提供模式:
55216 = closed 55217 = open read 55218 = open write fmClosed = $D7B0; fmInput = $D7B1; fmOutput = $D7B2; fmInOut = $D7B3;
在系统单元中搜索TTextRec以获取更多信息.
试试这个:
function IsFileInUse(fName: string) : boolean; var HFileRes: HFILE; begin Result := False; if not FileExists(fName) then begin Exit; end; HFileRes := CreateFile(PChar(fName) ,GENERIC_READ or GENERIC_WRITE ,0 ,nil ,OPEN_EXISTING ,FILE_ATTRIBUTE_NORMAL ,0); Result := (HFileRes = INVALID_HANDLE_VALUE); if not(Result) then begin CloseHandle(HFileRes); end; end;
这很好用:
function IsOpen(const txt:TextFile):Boolean; const fmTextOpenRead = 55217; fmTextOpenWrite = 55218; begin Result := (TTextRec(txt).Mode = fmTextOpenRead) or (TTextRec(txt).Mode = fmTextOpenWrite) end;