A reader emailed me with questions about configuration files. How to create them, and how to read from them, both during install time. I will cover how to create configuration files in this post, which is Part 1. I will cover how to read from configuration files in the next post, which will be Part 2.
Creating a configuration file is very easy in the InstallShield IDE, however it is different based on whether you are working in a Basic MSI or InstallScript project.
Here is how to create a configuration file in a Basic MSI project:
-
Go to the System Configuration group, INI File Changes view, and in the middle pane right-click INI Files and select Add INI File.
-
Rename the .ini file to whatever you want. In this case, it will be Mega View.ini. Now, if you click on the Mega View.ini node, you will see the properties for the file. You will see the Display Name, the Component the configuration file belongs to, and the Target folder, which by default will be [INSTALLDIR].
-
Next, right-click on the .ini file node (Mega View.ini) and select Add Section.
-
Rename the section to whatever you want. In this case, it will be general.
-
Next, right-click on the section node (general) and select Add Keyword.
-
Rename the keyword to whatever you want. In this case, it will be title.
-
Next, click on title, and in the right pane, set the Display Name, Action, and Data Value properties. Here are the values we will enter:
-
Display Name: title
-
Action: Replace Old Value
-
Data Value: Mega View – The Ultimate Viewer
-
Now, when you build and run your Basic MSI install, the Mega View.ini file will be created in the INSTALLDIR folder. The file will look like this:
[general]
title=Mega View – The Ultimate Viewer
That is how you create a configuration file in a Basic MSI project. Now, let's look at how to do it in an InstallScript project.
In an InstallScript project, you will have to create the configuration file from within the script. The function you use for this is WriteProfString(). Here is how the call to WriteProfString would look for Mega View.ini. We'll assume we want to write the same exact file that we did in the Basic MSI project:
if (WriteProfString(TARGETDIR ^ "Mega View.ini",
@IDS_MEGAVIEWINI_SECTION,
@IDS_MEGAVIEWINI_KEY,
@IDS_MEGAVIEWINI_VALUE) < 0) then
SprintfBox(SEVERE,"WriteProfString","Mega View.ini could not be created");
endif;
Let's look at the function parameters. TARGETDIR is the main installation folder for the InstallScript installation. Here we are building a path to the Mega View.ini file. Next, there are three parameters that are entries from the String Table. Let's look at each of these values:
IDS_MEGAVIEWINI_SECTION: general
IDS_MEGAVIEWINI_KEY: title
IDS_MEGAVIEWINI_VALUE: Mega View – The Ultimate Viewer
You also see a SprintfBox() call that displays an error message if the Mega View.ini file couldn't be created.
When the InstallScript installation is run, the Mega View.ini file will be created and will look the same as for the Basic MSI project.
That is how to create a configuration file in both Basic MSI and InstallScript projects. In Part 2, we will look at how to read a configuration file at install time.