Solved

modifying a file which is in use

Posted on 1998-08-28
4
185 Views
Last Modified: 2010-04-04
I would like to modify a file when it is in use. For example, if program1 has opened a flat file (file on local machine, not a database) and program2 wants to edit or save it, it gets an error.  Is there a way to do this in Delphi 3?  Sometimes I may just modify the file, other times I may remove it and rebuild it.  How can this be done in D3?

                   Thanks              
0
Comment
Question by:jexd99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Expert Comment

by:kjteng
ID: 1338029
The first program should open the file with fmReadOnly
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1338030
jexd99,

it depends on how the program that opened the file first (so that it is in use now). Look at "TFileStream.Create". You can open a file with different share modes. You should try to open the file with "fs:=TFileStream.Create(myFilePath,fmReadWrite or fmShareDenyNone)". But it will work only if the program that opened the file first opened it with this share mode, too. Otherwise you just cannot open it (at least with write access).

Regards, Madshi.
0
 
LVL 7

Accepted Solution

by:
Motaz earned 150 total points
ID: 1338031
Hello there..
If you open any file try to close it if the process was finished and reload it ( refresh ) to keep it up-to-date. If access denied error was raised then try to open it later ( for loop ) may be another program use it so that this program ( second one ) must use this file in very little time. Reading and writing large amount of date into files is very fast operation and it will take only fiew seconds ( except very large files, it may take minutes ).

Recommondations :

- Allways try to close data files after any operation.
- If you open a file for updating, deny others for reading it.
- If you open a file for updating try to close it as fast as posible.
- If you try to open a file and access denied error occured, try to open it several times and wait about one second between each open attemption.
- If you want to open a file for reading and you wouldn't need writing, set the FileMode variable to 0 ( Read only ) because if you write Reset without setting the file mode to 0, Delphi will open this file for read and write and will deny other programmes from accessing it.

e.g:

AssignFile(f,'MyData.Dat');
FileMode:=0;
Reset(f);



CloseFile(f);

Regards, Motaz from Sudan.

motaz1@yahoo

0
 

Author Comment

by:jexd99
ID: 1338032
Not quite what I wanted to do, as I should be able to access some API to break the hold on the file if necessary (just anything can be done if necessary :-), however this gives me some ideas to look into.  Thanks.  
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question