Example Code

Read all sections, keys, and values from any ini file Using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview
This example VI shows how to read an ini configuration file and extract all sections, keys, and values from it.


Description
This example will programmatically read an ini cofiguration file and extract all sections, keys, and values from it.  Each element will be stored as a string in one of three different arrays, respectively: "sections", "keys", or "values" array.


Requirements

  • LabVIEW 2012 (or compatible)


Steps to Implement or Execute Code

  1. (Optional) Save the test file config.ini in a directory on your computer.  You can use any ini file, but this was included for ease of testing.
  2. Open the attached LabVIEW 2009 vi called read_ini_file.vi.
  3. In the VI, point to the ini file of choice.
  4. Run the VI and observe the arrays "sections", "keys", and "values".

 

NOTE: If there are keys in the ini file without values, then these empty strings will not be added to the "values" array.  All arrays are created to ignore empty strings.

For more information on ini files, please read here: http://en.wikipedia.org/wiki/INI_file

 

Additional Information or References
VI Snippet

00.png

**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
jawier130
Member
Member
on
Hi, it can be possible real ini files with duplicate names of key??
rntaboada
Member
Member
on

The code is a solid starting point, but it has a significant limitation. Based on the three output arrays — one containing the section names, another the key names, and the third the corresponding values — there is no way to directly map sections to their associated key-value pairs. Specifically, the number of keys per section is not preserved, which could become an issue if there's a need to accurately reconstruct the original structure of the .ini file.

As a possible solution, I suggest changing the sections array to a 2-D array, where each row includes both the section name and the number of keys it contains. This would make it much easier to associate each section with its corresponding keys and values. While this is not the only solution — nor necessarily the best — it is effective and addresses the core problem.

Contributors