fmShareDenyWrite 模式似乎不起作用

发布于 2024-09-06 20:58:30 字数 491 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

源来凯始玺欢你 2024-09-13 20:58:30

不知道这是否已经是 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 类型的本地变量。

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 the

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

to

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

where myMode is a local var of type Word.

岁月打碎记忆 2024-09-13 20:58:30

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文