Solved

modifying a file which is in use

Posted on 1998-08-28
4
179 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
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now