Did you know that you can interact with INI files with ColdFusion? 

This tutorial will demonstrate how to write and read from INI files.

Now you may be saying, why would I want to interact with INI files, when I can use a database? Well, the answer is simple... If you are running software that runs off of an INI (i.e. an FTP server) you can easily interact with the FTP servers INI files to easily modify users information. In this example we will be using fictional INI file information. Let's begin:

The first thing I will show you is what an INI file looks like, let's imagine that this INI file is called settings.ini and is located in the following path (C:\windows\settings.ini):



The example above is a simple INI file, some tend to be a bit more complex, but this is just so you get the idea of how it's done ;)

If you wanted to read the values, you would use GetProfileString(INIPATH, SECTION, ENTRY).

The easiest way to do this is as follows:
    Username : #GetProfileString("C:\windows\settings.ini", "settings", "user")#<BR>
    Password : #GetProfileString("C:\windows\settings.ini", "settings", "password")#<BR>
    FTP Path : #GetProfileString("C:\windows\settings.ini", "settings", "path")#<BR>

The will display as follows:
    Username : John
    Password : Hello
    FTP Path : C:\John\

There, wasn't that easy? The next step is to actually write to the INI file and therefore make is useful :)

The write to an INI file, you will use the SetProfileString(inipath, section, entry, value).

Let's see an example of this:
   user = SetProfileString("C:\windows\settings.ini", "settings", "user", "#FORM.NEWUSERNAME#")
   Pass = SetProfileString("C:\windows\settings.ini", "settings", "user", "#FORM.NEWPASSWORD#")
   Path = SetProfileString("C:\windows\settings.ini", "settings", "user", "#FORM.NEWPATH#")

That will allow you to easily integrate your ColdFusion applications with other software titles that are INI driven! The best example as I mentioned before are FTP servers (a few you can try Serv-U, G 6 and there are a few more!)

Well, you now understand the concept of GetProfileString() & SetProfileString()..... Questions? Comments? Email Me....

About This Tutorial
Author: Pablo Varando
Skill Level: Intermediate 
Platforms Tested: CF5
Total Views: 142,546
Submission Date: October 04, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial

Website Designed and Developed by Pablo Varando.