fmShareDenyWrite 模式似乎不起作用
我正在使用 TFileSteam 打开日志文件。我希望能够从其他进程读取此日志文件。我认为 fmShareDenyWrite 模式允许这样做。
但是,如果我尝试从其他进程打开该文件,则会收到错误。例如,如果我尝试从命令行键入文件,我会得到“该进程无法访问该文件,因为它正在被另一个进程使用”。
这是文件初始化代码:
if FileExists(AutoLogFileName) then
_ActivityLogStream := TFileStream.Create(AutoLogFileName,
fmOpenReadWrite or fmShareDenyWrite)
else
_ActivityLogStream := TFileStream.Create(AutoLogFileName,
fmCreate or fmShareDenyWrite);
注意: 我使用的是 Delphi 版本 6。
I'm using a TFileSteam to open a log file. I would like to be able to read through this log file from other processes. I thought the fmShareDenyWrite mode would allow this.
However if I try to open the file from other processes, I get an error. For example, if I try and type the file from the command line, I get "the process can not access the file because it is being used by another process".
Here is the file initialization code:
if FileExists(AutoLogFileName) then
_ActivityLogStream := TFileStream.Create(AutoLogFileName,
fmOpenReadWrite or fmShareDenyWrite)
else
_ActivityLogStream := TFileStream.Create(AutoLogFileName,
fmCreate or fmShareDenyWrite);
NOTE:
I am using Delphi version 6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道这是否已经是 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) 构造函数(有两个重载构造函数)并自行处理模式参数即可。复制原始构造函数中的代码,并将 更改为
其中
myMode 是 Word 类型的本地变量。
Don't know whether this was already a bug in D6, but that is a distinct possibility. There is a QC report on this reported against D2007: QC 65767: http://qc.embarcadero.com/wc/qcmain.aspx?d=65767. This report is now closed, as it was resolved in D2010 (14.0.3467.22472 to be exact).
Update (prompted by Gabr's comment):
You can create your own TFileStream descendant that does honor the mode. Just override the
Create(const AFileName: string; Mode: Word; Rights: Cardinal)
constructor (there are two overloaded constructors) and handle the mode parameter yourself. Copy the code from the original constructor and change theto
where myMode is a local var of type Word.
mfCreate 模式对于任何共享属性都无法正常运行/工作。要解决此问题,您必须自己创建文件句柄并将其传递给构造函数
Cheer
mfCreate mode does not behave/work correctly with any share attribute. To work around, you must create file handle yourself and pass it to the constructor
Cheer