[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

How to open a file in read-write mode, and lock out others

In Delphi VCL, I am reading a cds file into a TclientDataSet, and I don't want anyone else to be able to open it.

If I use this code, I cannot even open the file - it gives an error:

  result := true;
  try
    StreamWords := TFileStream.create(fMMain.cdsWords.FileName,
      fmOpenReadWrite or fmShareExclusive);
    fMMain.cdsWords.LoadFromStream(streamWords);
  except
    StreamWords.Free;
    ShowMessage('Cannot open Words.cds file - may be already being used.');
    result := false;
    exit;
  end;

Open in new window


If I use this code, it opens it OK, but it doesn't stop the same code re-opening the file.

  result := true;
  try
    StreamWords := TFileStream.create(fMMain.cdsWords.FileName,
      fmOpenRead or fmShareDenyWrite);
    fMMain.cdsWords.LoadFromStream(streamWords);
  except
    StreamWords.Free;
    ShowMessage('Cannot open Words.cds file - may be already being used.');
    result := false;
    exit;
  end;

Open in new window


It should be simple, but I seem to be making hard work of it.

Help please!
0
GrahamDLovell
Asked:
GrahamDLovell
  • 2
1 Solution
 
GrahamDLovellAuthor Commented:
I have found a solution to my reported problem.

I have got the first set of code to work. I just had the file name loaded into the cds file FileName field, and processing associate with that is what was taking the lock that wouldn't let the Stream open the file.

The second execution of the program now fails, but I now get ungainly exit on the exception. I will put this in a separate post if I can't solve it.
0
 
GrahamDLovellAuthor Commented:
Reported problem no longer exists, as I found my own "simple mistake."
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now