Solved

modifying a file which is in use

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Working this component WebCopy with proxy, help 3 100
Strange behavior when a form is closed 6 68
Firemonkey android show image from resource ? 1 56
update joined tables 2 62
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 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