作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我正在使用 TFileSteam 打开日志文件。我希望能够从其他进程读取此日志文件。我认为 fmShareDenyWrite 模式允许这样做。 但是,如果我尝试从其他进程打开该文件,则会收到错误。例如
我是一名优秀的程序员,十分优秀!