Visual Basic Programmer's Resources/Examples:

Reading/Writing Profile Strings:

    
' Instructions:

' 1) Create a new project with one form and one module.
' 2) Add a command button to the form.
' 3) Add two radio buttons to the form
' 4) Set the radio button properties as follows:
'    Option1.value = True
'    Option1.caption = "getIni"
'    Option2.caption = "putIni"
' 5) Paste the following code as indicated.
' 6) To test, run the project:
'    A) Click Option2 (putIni)
'    B) For .ini file enter "junk.ini" (without the quotes)
'    C) For Section enter "Section" (without the quotes)
'    D) For Key enter "Key" (without the quotes)
'    E) For Value enter "Value" (without the quotes)
'    F) Click Command1.
'    G) Use Notepad to navigate to the Windows directory and open junk.ini.
'    H) You will find the file junk.ini has been created with
'       the following entries:
'
' [Section]
' Key=Value
'
'    I) To read this value from the file, click Option1 (getIni)
'       and enter the appropriate information when prompted. The
'       value will be displayed.


'  ------------- Paste this code into Module1 -------------

'  This sample code is presented as is.
'  Although every reasonable effort has been
'  made to insure the correctness of the example
'  below, Idioma Software Inc. makes no warranty
'  of any kind with regard to this program sample
'  either implicitly or explicitly.

'  This program example may be freely distributed for the
'  use of writing computer programs only. Any other use of
'  this material requires written permission from Idioma Software inc.
'  (c) 2000 Idioma Software inc. All rights reserved.
 
' Title:    Using VB to show a web page on the net.
' Platform: Visual Basic - 32 bit
' Author:   Jon Vote
' Contact:  jon@idioma-software.com
' Date:     03/00
'

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
   
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 getIni(sSection As String, sKey As String, sFile As String) As String

'Retrieves an entry from an .ini file:

    Dim sTemp As String
    Dim rc As Long

    'Always be sure to initialize a string being passed to an API
    sTemp = Space$(256)
    rc = GetPrivateProfileString(sSection, sKey, "", sTemp, Len(sTemp), sFile)
    
    'rc has the length of the string. Be sure to trim anything to the right
    sTemp = Left$(sTemp, rc)
    
    getIni = sTemp

End Function
Sub PutIni(ByVal sSection As String, ByVal sKey As String, ByVal sEntry As String,
sFile As String)

' puts an entry to the .ini file

    Dim rc As Long
    
    rc = WritePrivateProfileString(sSection, sKey, sEntry, sFile)
 
End Sub




'  ------------- End code for Module1 -------------





'  ------------- Paste this code into Form1 -------------



Private Sub Command1_Click()

    Dim sIniFile As String
    Dim sSection As String
    Dim sKey As String
    Dim sValue As String
    
    sIniFile = InputBox$(".ini file?")
    sSection = InputBox$("Section?")
    sKey = InputBox("Key?")
    
    If Option1.Value Then 'getIni
      sValue = getIni(sSection, sKey, sIniFile)
      MsgBox "The value is: " & sValue
    Else 'putIni
      sValue = InputBox("Value?")
      Call PutIni(sSection, sKey, sValue, sIniFile)
    End If 'Option1.value
    
End Sub



'  ------------- End code for Form1 -------------

 

 
 
 
 
 

Powered by
Guatemalan Dark Roast