Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
10 ratings:
 2.10 out of 5   

How Can I Check if a File is Already Open?

Primary Software: LabVIEW Development Systems>>LabVIEW Professional Development System
Primary Software Version: 1.0
Primary Software Fixed Version: N/A
Secondary Software: N/A

When I use a file that is shared over a network, I sometimes need to know if another program already has the file open. Knowing this will help me avoid race conditions when I have multiple LabVIEW applications writing to the same file. How can I determine if the file is already in use?

In LabVIEW the Open/Create/Replace VI doesn’t generate any error or warning if you try to open a file that is already open. If you have many applications that want to write to the same file, you have to check that they don’t access the file simultaneously in order to avoid race conditions.

If these applications are on the same PC you can use semaphores to regulate the access to the shared file. See the Community Example link below for an example.

Note: Semaphores will not work if your applications run on different PCs on a network (see related links below for more information). In this case you can bypass this limitation with the following workaround, as shown in the attached example:
  1. When each application opens the shared file, you simultaneously create a temporary file.
  2. In each application you destroy the temporary file when the application closes the shared file.
  3. In order to know if one of the applications is currently accessing to the shared file, you can check if the temporary file exists with the Check if File or Folder Exists VI.
You can use the same workaround also in CVI (see the link below for the functions you can use to check the existence of the temporary file).

Related Links:
KnowledgeBase DIRECT-3S8RDV: What Is a Semaphore?  
KnowledgeBase 3BRIBK60: Using CVI to Programmatically Check File or Directory Existence
NI Community Example: Protecting Config File Access With Semaphores

Protect Shared

Report Date: 09/28/2009
Last Updated: 09/29/2017
Document ID: 51R2HCRZ

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit