Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is file already open?
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 10
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01627882
Message ID:
01628010
Vues:
53
An alternate method I have used that also works with most files (as far as I can tell) is to use the function below. See the caveats. And note, I think the USE table EXCLUSIVE works just fine if it is a table. Here is my function - fairly old, got the idea from someone else (sorry, did not document) and so might not be bullet-proof.
PROCEDURE CanOpenFileExcl
* procedure to see if a file can be opened exclusively by opening the file with FOPEN(); can be used
* with any file; note that file is closed after opening so it is an "instantaneous" check i.e. the
* file could still be locked after this function returns; note also that if the file does not exist, it also
* returns .F. as this is a test if it can be opened: so should test for file existence before calling

* Big Note: you are going to have to check to see if the function really does detect if the file is open
* or not; the code below opens in read/write unbuffered mode so that it fails to open a file that cannot
* be shared in this way; but some files can [could?] be opened shared like this even if they are opened by
* another application: do your own testing...I have found the following so far:

* 1) a .dbf file that is not open already: returns .T.
* 2) a .dbf file that is open already in shared mode or exclusive mode: returns .F.
* 3) a .pdf file that is open in the Acrobat viewer: returns .F.
* 4) a .xls file that is open in Excel opens .F.

* Written by: Albert Gostick
* Last Updated: Aug 18, 2006

* Changes:
* Aug 18, 2006: noticed that tcFileName was "tnFileName" so changed throughout
* Dec 3, 2004: original coding

* Parameters:
* tcFileName: full file name and path i.e. path is required
* Returns: .T. if file can be opened exclusively, .F. otherwise

LPARAMETERS tcFileName

LOCAL lnHandle, llReturn

* default return value
STORE .F. TO llReturn

* check that file exists; if it does not exist, then we should return .F.
IF NOT FILE(tcFileName)
   RETURN llReturn
ENDIF

* try to open the file in read/write unbuffered mode as this fails if file is open by another application
* (even within FoxPro) if it cannot be shared

STORE FOPEN(tcFileName,12) TO lnHandle

* if lnHandle is -1, it could not be opened; if it was opened, file handle will be a positive value
IF lnHandle > 0

   * file was opened; close it and set return value
   FCLOSE(lnHandle)
   STORE .T. TO llReturn

ENDIF

RETURN llReturn
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform