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

  • SEARCH
  • Home
  • 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 6053335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:01:45+00:00 2026-05-23T08:01:45+00:00

How can I deny access (only to write) to a file for other processes?

  • 0

How can I deny access (only to write) to a file for other processes? I will read\write a file all time.

I use

FileOpen('c:\1.txt', fmOpenReadWrite or fmShareDenyWrite)

but after (starting to load the file to StringList) I get error

Cannot open file C:\1.txt. The process cannot access the file because it is being used by other process.”

Only I open the file.

  • 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-23T08:01:45+00:00Added an answer on May 23, 2026 at 8:01 am

    Here, the error message is actually slightly misleading. The reason you can’t load into the stringlist is because you already opened the file in read/write.

    if you check the implementation of TStrings.LoadfromFile:

    procedure TStrings.LoadFromFile(const FileName: string);
    var
      Stream: TStream;
    begin
      Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
      try
        LoadFromStream(Stream);
      finally
        Stream.Free;
      end;
    end;
    

    You see that it tries to open the file with a “DenyWrite” condition, but you already opened the file in write mode. That is the reason why it fails.

    You can work around that by using LoadFromStream instead.

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Stream: TStream;
    begin
      Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
      try
        Stringlist.LoadFromStream(Stream);
      finally
        Stream.Free;
      end;
    end;
    

    Note that you will need to use fmShareDenyNone for this to work in that situation. Then again, you could probably reuse the Read/Write handle you got from your OpenFile, probably do something like this:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Stream: TStream;
      iPosition : Int64;
    begin
      Stream := THandleStream.Create(FHandle); //FHandle is the read/write handle returned by OpenFile
      try
        iPosition := Stream.Position;
        Stream.Seek(0, soFromBeginning);
        Stringlist.LoadFromStream(Stream);
        Stream.Position := iPosition;
        //Restore stream position.
      finally
        Stream.Free;
      end;
    end;
    

    But be advised that these approach might have a few “gotchas” I’m unaware of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I deny access to a file I open with fstream? I want
I want to allow access to a file (secret.txt) only from my ip. Below
How can one deny access to all subdirectories of a given directory? (While allowing
Is it possible to deny from all apache htaccess style using php. I can't
How can I deny access to particular directory in IIS. In Apache I could
I have the following .htaccess file: Order deny,allow Deny from all Allow from XX.XXX.XXX.XXX
I'm trying to deny all and allow only for a single IP. But, I
I use the following code to only read the database.... NSString *databasePath = [[[NSBundle
I'm currently trying to deny access to a specific html-document which should only be
Need to execute potentially dangerous binary file and want to deny access any hardware.

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.