Solved

modifying a file which is in use

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 97
Delphi: how to implement a User Shortcut mapper? 1 107
Delphi - replicating a form 8 72
update joined tables 2 45
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

14 Experts available now in Live!

Get 1:1 Help Now