gpt4 book ai didi

delphi - fmShareDenyWrite 模式似乎不起作用

转载 作者:行者123 更新时间:2023-12-03 15:07:18 25 4
gpt4 key购买 nike

我正在使用 TFileSteam 打开日志文件。我希望能够从其他进程读取此日志文件。我认为 fmShareDenyWrite 模式允许这样做。

但是,如果我尝试从其他进程打开该文件,则会收到错误。例如,如果我尝试从命令行键入文件,我会得到“该进程无法访问该文件,因为它正在被另一个进程使用”。

这是文件初始化代码:

if FileExists(AutoLogFileName) then
_ActivityLogStream := TFileStream.Create(AutoLogFileName,
fmOpenReadWrite or fmShareDenyWrite)
else
_ActivityLogStream := TFileStream.Create(AutoLogFileName,
fmCreate or fmShareDenyWrite);

注意:我使用的是 Delphi 版本 6。

最佳答案

不知道这是否已经是 D6 中的一个错误,但这是一种明显的可能性。有一份针对 D2007 的 QC 报告:QC 65767:http://qc.embarcadero.com/wc/qcmain.aspx?d=65767 。该报告现已关闭,因为它已在 D2010 中得到解决(确切地说是 14.0.3467.22472)。

更新(由 Gabr 的评论提示):

您可以创建自己的 TFileStream 后代来遵循该模式。只需覆盖 Create(const AFileName: string; Mode: Word; Rights: Cardinal)构造函数(有两个重载构造函数)并自己处理模式参数。从原始构造函数中复制代码并更改

  if Mode = fmCreate then
begin
inherited Create(FileCreate(AFileName, Rights));

  if (Mode and fmCreate = fmCreate) then
begin
myMode := Mode and $FF;
if myMode = $FF then
myMode := fmShareExclusive;
inherited Create(FileCreate(AFileName, myMode, Rights));

其中 myMode 是 Word 类型的本地变量。

关于delphi - fmShareDenyWrite 模式似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3128770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com