代码之家  ›  专栏  ›  技术社区  ›  Aheho

fmShareDenyWrite模式似乎不起作用

  •  5
  • Aheho  · 技术社区  · 14 年前

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

    以下是文件初始化代码:

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

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

    2 回复  |  直到 14 年前
        1
  •  8
  •   Marjan Venema    14 年前

    不知道这是否已经是D6中的一个错误,但这是一个明显的可能性。根据D2007:QC 65767: http://qc.embarcadero.com/wc/qcmain.aspx?d=65767 . 此报告现已关闭,因为它在2010年2月解决(确切地说是14.0.3467.22472)。

    您可以创建自己的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));
    

        2
  •  2
  •   APZ28    14 年前

    mfCreate模式与任何共享属性的行为/工作不正确。要解决这个问题,您必须自己创建文件句柄并将其传递给构造函数

    欢呼