08-27-2021 08:51 AM
Hi everyone,
I faced recently something strange with Config File. In my Software, a key is updated according to a condition, but I noticed that the MyConfig.ini is not updated if it's not closed. I mean, in LabVIEW the file is opened and the data is written to it. But If I open the file in Windows, I see it's still not updated. If I close the file in LabVIEW, then it will be updated.
I am wondering, if this is right?
Solved! Go to Solution.
08-27-2021 09:00 AM
Yes, read the detailed help for the Write Key VI.
08-27-2021 09:12 AM
@Sinan_Ismael wrote:
Hi everyone,
I faced recently something strange with Config File. In my Software, a key is updated according to a condition, but I noticed that the MyConfig.ini is not updated if it's not closed. I mean, in LabVIEW the file is opened and the data is written to it. But If I open the file in Windows, I see it's still not updated. If I close the file in LabVIEW, then it will be updated.
I am wondering, if this is right?
That sounds right to me... Usually Windows will lock a file when one program has it open. Then if another program tries to open that file it will get a sharing violation and Windows would usually pop up a box asking if the second program wants to open it in read only mode or open a copy of the file.
I am only guessing because I use XML for all my config files, but It's also possibly that file changes are not written to the file until it is closed. This could be a function of the LabVIEW config file VI's.
08-27-2021 09:54 AM
Yes, that is correct, only the close file will flush the changes from memory to file.
The close file even has an option to discard all the changes, it is possible only if it is flushing at close.
08-27-2021 11:21 AM
Just to add some details...
The Configuration File reference really just holds lookup tables. As you add to the configuration, only those lookup tables are updated (in memory). The file itself is only touched when opening (read the file to parse it and build the tables) and closing (replace the file using the contents in the tables).
08-27-2021 02:36 PM
I tend to make my config file writes atomic because I don't want to deal with what happens if my code crashes before the changes were written. I mean, I know I can include closing the config in the cleanup state, but it's usually unimportant to me to have the config file reference remain open, so why not "open, write, close" all at once.
08-27-2021 08:17 PM
I have rarely needed write to configuration files. I will edit them in a text editor and read the configuration at application start up. Granted, over the last couple of years, I have moved on to using XML and/or JSON for configuration data (more commonly JSON). The main reason is I found I really needed a hierarchy for defining my instruments.
09-06-2021 01:59 AM
Yes, you are right.
09-07-2021 04:22 AM
If you install the "Hidden Gems in vi.lib" tool from the VI Package Manager you will find a VI called LV Config Write String.vi in the Files menu. That VI will write the key to file instantly, but you will have to provide the value to write as a string.
Now if you open the Close Config File.vi you can see how it converts the file refnum to a queue and then within the Save Config File.vi it also calls it has a function that acts on that queue to generate the needed strings....which also allows you access to the Config to String.vi which shows you the type of the queue and how to extract the key value...So you have all you need to intervene in a number of ways here. You could combine the latter VI with the hidden gem one, or rewrite the Save Config File-subVI of close config file to do it.
09-07-2021 11:15 AM
As I never see a reason to keep the reference to the config file alive, I just open it, do reads and/or writes to it, then close it all at once. No need to hold the reference open and then have to worry about closing it, should something untoward happen.