01-21-2010 09:01 AM
Hello,
I am using LV8.6.1 and writing this application that read and write to a text file. The application is being used by many users at different sites and I want to prevent writing to the text file when other user is already using it (reading or writing to it). The other user should only be able to use it when it's not being read or getting written.
Any suggestions? I know LV file function 'Set Permissions' won't work. I need a file attribute that tells that the file is open, so don't use it.
01-21-2010 09:09 AM
I think the only real obtion is one I've seen some other programs use.
Create a lock file in the same directory.
1. You want to open myfile.txt.
2. Your program looks to see if myfile.lck is present. (Same filename, just the extension is different)
3. If it is present, you don't open the file or you give a warning.
4. If it is not present, you create a myfile.lck file. It could be a small text file. Perhaps you place in it the user name of the person who is opening the file.
5. When your VI is done with the file and "closes" it, your VI deletes the myfile.lck file.
So the presence of the myfile.lck file indicates the file is being used.
The absence of the myfile.lck indicates the file is available to be used.
The only risk is if somehow the person who "takes out" the file somehow ends their application unexpectedly and the .lck file doesn't get deleted even though they are done. You would need to manually go in and delete the .lck file. But username data in the .lck could help determine who had the file last and you could confirm if they are really using it or not. Putting the filedate in the warning information would help determine if it was a recent lock, or an old lock that is likely stale.
01-21-2010 09:20 AM
Another option would be to simply set and check the read/write property in the OS file system using the File Access Rights vi.