?
Solved

modifying a file which is in use

Posted on 1998-08-28
4
Medium Priority
?
187 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 450 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 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