Main Topics
Browse All TopicsHi All,
I would like to write a vb program for updating ini file.
Sample code is advantage.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here goes the code. Just paste in a visual basic form and you can read and write from a vb app.
code starts---------------
Option Explicit
'Win APi declarations
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA"
Private Declare Function WritePrivateProfileSection
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileString
Private Sub Form_Load()
Dim Str As String
Open "c:\Sample.txt" For Output As #1
Close #1
Dim m_File As String
m_File = "C:\Sample.txt"
'write to file
WriteIniSection m_File, "SampleTest1", ""
WriteIniSection m_File, "SampleTest2", "Here shoud be found some text"
WriteIni m_File, "TestSample3", "Ini1", "This is ini file 1"
WriteIni m_File, "TestSample1", "Ini2", "This is ini file 2"
'read from the file
Str = Str & "SampleTest2 section: " & vbTab & ReadIniSection(m_File, "SampleTest2") & vbCrLf
Str = Str & "SampleTest1 section: " & vbTab & ReadIniSection(m_File, "SampleTest1") & vbCrLf
Str = Str & "Ini1 string: " & vbTab & ReadIni(m_File, "TestSample3", "Ini1") & vbCrLf
Str = Str & "Ini2 string: " & vbTab & ReadIni(m_File, "TestSample1", "Ini2") & vbCrLf
End
End Sub
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Se
ReadIni = Left(RetVal, v - 1)
End Function
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(S
End Function
Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub
Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
WritePrivateProfileSection
End Sub
Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA"
Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileString
Public Function ProfileSaveItem(lpSectionN
' **************************
' * PURPOSE: This function saves the passed value to the file *
' * under the section and key names specified *
' * PARAMETERS: lpSectionName -> name of the section we want to create (or adjust) *
' * lpKeyName -> name of the keyname we want to create (or adjust) *
' * in the section given in lpSectionName *
' * lpValue -> The value of the KeyName *
' * IniFile -> Path and name of the ini-file we want to adjust *
' * RETURN: No return-value *
' * REMARK: - If the ini file does not exist, it is created. *
' * - If the section does not exist, it is created. *
' * - If the key name does not exist, it is created. *
' * - If the key name exists, it's value is replaced. *
' **************************
On Error GoTo ProfileSaveItemError
Call WritePrivateProfileString(
Exit Function
ProfileSaveItemError:
MsgBox "ProfileSaveItem: " & Err.Number & " " & Err.Description, vbOKOnly + vbCritical
End Function
Public Function ProfileGetItem(lpSectionNa
lpKeyName As String, _
DefaultValue As String, _
IniFile As String, _
Optional nWantedValue As Long = 0) As String
' **************************
' * PURPOSE: Retrieves a value from an ini file corresponding *
' * to the section and key name passed. *
' * PARAMETERS: lpSectionName -> name of the section we want to create (or adjust) *
' * lpKeyName -> name of the keyname we want to create (or adjust) *
' * in the section given in lpSectionName *
' * DefaultValue -> if nothing is returned from the ini-file, this *
' * value will be given back to the calling procedure *
' * IniFile -> Path and name of the ini-file we want to adjust *
' * nWantedValue -> Do we need the full keyvalue or not ? *
' * RETURN: The return value is the length of the string in ret, including the *
' * terminating null. If a default value was passed, and the section or *
' * key name are not in the file, that value is returned. *
' * If no default value was passed (""),then success will = 0 if not found.*
' **************************
On Error GoTo ProfileGetItemError
' Declaration of the variables
Dim success As Long
Dim nSize As Long
Dim ret As String
Dim nPos As Long
Dim cTmp As String
Dim nValue As Long
' Pad a string large enough to hold the data.
ret = Space$(2048)
nSize = Len(ret)
success = GetPrivateProfileString(lp
If success Then
If nWantedValue = 0 Then
'//// Full Keyvalue wanted ////
ProfileGetItem = left$(ret, success)
Else
cTmp = left$(ret, success)
'//// skip preceding keydata ////
For nValue = 1 To (nWantedValue - 1)
nPos = InStr(1, cTmp, ",", vbTextCompare)
If nPos = 0 Then Exit For
cTmp = Mid(cTmp, nPos + 1)
Next
If nValue = nWantedValue Then
'//// Wanted value is reached ////
nPos = InStr(1, cTmp, ",", vbTextCompare)
If nPos <> 0 Then cTmp = Mid(cTmp, 1, nPos - 1)
Else
'//// Wanted value is not available ////
cTmp = ""
End If
ProfileGetItem = cTmp
End If
End If
Exit Function
ProfileGetItemError:
MsgBox "ProfileGetItemError: " & Err.Number & " " & Err.Description, vbOKOnly + vbCritical
End Function
Public Function ProfileDeleteItem(lpSectio
' **************************
' * PURPOSE: This function will remove the keyname and its corresponding value *
' * from the section specified in lpSectionName. *
' * This is accomplished by passing vbNullString as the lpValue parameter.*
' * PARAMETERS: lpSectionName -> name of the section we want to create (or adjust) *
' * lpKeyName -> name of the keyname we want to remove *
' * in the section given in lpSectionName *
' * IniFile -> Path and name of the ini-file we want to adjust *
' * RETURN: No return-value *
' * EXAMPLE: assuming that an ini file had: *
' * [Colours] *
' * Colour1=Red *
' * Colour2=Blue *
' * Colour3=Green *
' * and this sub was called passing "Colour2" 'as lpKeyName, *
' * the resulting ini file would contain: *
' * [Colours] *
' * Colour1=Red *
' * Colour3=Green *
' **************************
On Error GoTo ProfileDeleteItemError
Call WritePrivateProfileString(
Exit Function
ProfileDeleteItemError:
MsgBox "ProfileDeleteItemError: " & Err.Number & " " & Err.Description, vbOKOnly + vbCritical
End Function
Public Function ProfileDeleteSection(lpSec
' **************************
' * PURPOSE: This function will remove the entire section corresponding to *
' * lpSectionName.This is accomplished by passing vbNullString as both *
' * the lpKeyName and lpValue parameters *
' * PARAMETERS: lpSectionName -> name of the section we want to remove *
' * IniFile -> Path and name of the ini-file we want to adjust *
' * RETURN: No return-value *
' * EXAMPLE: assuming that an ini file had: *
' * [Colours] *
' * Colour1=Red *
' * Colour2=Blue *
' * Colour3=Green *
' * and this sub was called passing "Colours" as lpSectionName, *
' * the resulting Colours section in the ini file would be deleted *
' **************************
On Error GoTo ProfileDeleteSectionError
Call WritePrivateProfileString(
Exit Function
ProfileDeleteSectionError:
MsgBox "ProfileDeleteSectionError
End Function
Business Accounts
Answer for Membership
by: a_pravarakhyaPosted on 2003-10-29 at 04:46:14ID: 9641320
See this code for reading and writing into INI file from VB:
(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 A" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long p.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA"
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileString
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim Ret As String, NC As Long
'Write the setting to the file (c:\test.ini) under
' Project1 -> Keyname
WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"
'Create a buffer
Ret = String(255, 0)
'Retrieve the string
NC = GetPrivateProfileString(Ap
'NC is the number of characters copied to the buffer
If NC <> 0 Then Ret = Left$(Ret, NC)
'Show our string
MsgBox Ret
'Delete the file
Kill "c:\test.ini"
End Sub