winzip icon

A Beginner's INI Tutorial

Submitted on: 1/29/2015 7:39:00 PM
By: VBScript (from psc cd)  
Level: Beginner
User Rating: By 29 Users
Compatibility: VB 6.0
Views: 4395
     This is a simple project showing how to work with an INI file. It checks for the existence of the file, creates the file if needed, reads the settings and displays them. It allows writing new settings, refreshing the display on the fly. Please, comment on this if you find it useful, and as with all my submissions, I am not out for votes.


Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'Windows API/Global Declarations for :A Beginner's INI Tutorial
' This module makes writing to an INI file very easy. See the examples
' below for what needs to go into the VB code for this to work.
' Write Example:
' --------------
' WriteINI "Section", "Setting", Value, App.Path & "\settings.ini"
' Read Example:
' -------------
' Variable = ReadINI("Section", "Setting", App.Path & "\settings.ini")
Option Explicit
Public Declare Function getprivateprofilestring Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpdefault As String, ByVal lpreturnedstring As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function writeprivateprofilestring Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Function ReadINI(Section As String, KeyName As String, FileName As String) As String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, getprivateprofilestring(Section, ByVal KeyName$, "", sRet, Len(sRet), FileName))
End Function
Function WriteINI(sSection As String, sKeyName As String, sNewString As String, sFileName) As Integer
Dim r
r = writeprivateprofilestring(sSection, sKeyName, sNewString, sFileName)
End Function
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net

Other 15 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.