checking if a file is locked before calling the reset(file) function

hi guys,

i want to open a text file in exclusive mode. in other words, if something else uses it currently, it should skip and try the next file.

what i do is:

assignfile(data,'c:data.txt');
filemode=fmOpenReadWrite + fmShareExclusive;
reset(data);

i wrote another program that will hold that other file .. (only a assign file and reset.)

then i try to assign the same file in another program.

it doesn't fail at all, and i think it should.

can someone please help me.

what i am trying to do, before i call reset, check another program uses it?

regards
ripper


Comments

  • : hi guys,
    :
    : i want to open a text file in exclusive mode. in other words, if something else uses it currently, it should skip and try the next file.
    :
    : what i do is:
    :
    : assignfile(data,'c:data.txt');
    : filemode=fmOpenReadWrite + fmShareExclusive;
    : reset(data);
    :
    : i wrote another program that will hold that other file .. (only a assign file and reset.)
    :
    : then i try to assign the same file in another program.
    :
    : it doesn't fail at all, and i think it should.
    :
    : can someone please help me.
    :
    : what i am trying to do, before i call reset, check another program uses it?
    :
    : regards
    : ripper
    :
    :
    I think you should use the TFileStream for that. It is a little more difficult to read but it can fail if the file is in use.
  • : : hi guys,
    : :
    : : i want to open a text file in exclusive mode. in other words, if something else uses it currently, it should skip and try the next file.
    : :
    : : what i do is:
    : :
    : : assignfile(data,'c:data.txt');
    : : filemode=fmOpenReadWrite + fmShareExclusive;
    : : reset(data);
    : :
    : : i wrote another program that will hold that other file .. (only a assign file and reset.)
    : :
    : : then i try to assign the same file in another program.
    : :
    : : it doesn't fail at all, and i think it should.
    : :
    : : can someone please help me.
    : :
    : : what i am trying to do, before i call reset, check another program uses it?
    : :
    : : regards
    : : ripper
    : :
    : :
    : I think you should use the TFileStream for that. It is a little more difficult to read but it can fail if the file is in use.
    :
    what i done was the following:

    openForReading := fileopen(filename,fmOpenReadWrite);
    if openForReading < 0 then
    begin
    FileClose(openForReading);
    don't process @ all, because it has a lock on
    end
    else
    assignfile(...
    reset(..

    read file ....

    thanks again

    regards
    ripper

  • : hi guys,
    :
    : i want to open a text file in exclusive mode. in other words, if something else uses it currently, it should skip and try the next file.
    :
    : what i do is:
    :
    : assignfile(data,'c:data.txt');
    : filemode=fmOpenReadWrite + fmShareExclusive;
    : reset(data);
    :
    : i wrote another program that will hold that other file .. (only a assign file and reset.)
    :
    : then i try to assign the same file in another program.
    :
    : it doesn't fail at all, and i think it should.
    :
    : can someone please help me.
    :
    : what i am trying to do, before i call reset, check another program uses it?
    :
    : regards
    : ripper


    I have this code in my codelibrary, maybe it helps:[code]
    function IsFileInUse( Const FileName: String ): Boolean;
    var
    HFileRes: HFILE;
    begin
    Result := False;
    If not FileExists( FileName ) then Exit;
    HFileRes := CreateFile( PChar( FileName ),
    GENERIC_READ or GENERIC_WRITE, 0, nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
    Result := ( HFileRes = INVALID_HANDLE_VALUE );
    If not Result then
    CloseHandle( HFileRes );
    end;[/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories