Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5956857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:19:03+00:00 2026-05-22T18:19:03+00:00

until now, whenever I wanted to create a file without overwriting an existing one,

  • 0

until now, whenever I wanted to create a file without overwriting an existing one, I’ve done something like this:

if not FileExists(filename) then
  stream := TFileStream.Create(filename, fmCreate);

But that’s not threadsafe. So now I am looking for a threadsafe version.

Perhaps I can combine some modes so that TFileStream.Create(filename, fmCreate +fm???); fails if the file exists?

I need this to communicate directory-locks with old DOS programs. But the DOS programs don’t hold the files opened. 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:19:04+00:00Added an answer on May 22, 2026 at 6:19 pm

    TFileStream‘s file-name based constructor relies on the WIndows API call CreateFile to create the file handle that’ll be used to access the file. The API itself has multiple parameters, and especially interesting for you is the Create Disposition: if you specify CREATE_NEW, the function fails if the file already exists. You can take advantage of that by calling CreateFile yourself, then using the returned handle to create the TFileStream. You can do that because TFileStream inherits from THandleStream, inherits it’s handle-based constructor and owns the handle (calls CloseHandle on the handle you pass to the constructor).

    Since this relies on the OS-provided function CreateFile, it’ll be trehad-safe (no race condition between FileExists() and actually creating the file. It also blocks the old application from accessing the newly crearted file until you actually close the handle.

    var FH: NativeUInt;
    
    // ...
    
      FH := CreateFile('FileName', GENERIC_READ or GENERIC_WRITE, 0, nil, CREATE_NEW, 0, 0);
      if FH = INVALID_HANDLE_VALUE then
      begin
        // Creating the new file failed! I'm raizing an exception, but you can do something
        // better-suited for your case, like trying a new file name.
        RaiseLastOSError;
      end;
      // Create the stream using the prepared Handle
      HS := TFileStram.Create(FH);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Up until now we used Ant in my company. Whenever we wanted to send
I came across this recently, up until now I have been happily overriding the
I'm a LaTeX beginner. Up until now whenever I've created a table (using the
I haven't think of this yet now . Till now whenever device was asking
Up until now I have been using std::string in my C++ applications for embedded
I'm a ClearCase newbie and up until now have been used to SVN. Therefore,
I am introducing automated integration testing to a mature application that until now has
From the .net 4.0 previews I have read until now there has been lots
I am new to jQuery.just learn and use it for 3 weeks until now.
I've never really had the need to use any drag functions until now, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.