INI Files

P

Pepi Tonas

Hi Y'all

This probably a century old question in this NG for the newborn .NET,
but still I miss them.

Is there a way to use INI files from VB.NET. I know there are
alternate solutions such as XMLs, but due to legacy support, I would
like to try to read and write into INI files. I reckon that through
the use of file functions I can do this, but was more thinking about
some readymix kinda solution, maybe through API calls, but which?

Thanx.
 
H

Herfried K. Wagner [MVP]

* Pepi Tonas said:
This probably a century old question in this NG for the newborn .NET,
but still I miss them.

Is there a way to use INI files from VB.NET. I know there are
alternate solutions such as XMLs, but due to legacy support, I would
like to try to read and write into INI files. I reckon that through
the use of file functions I can do this, but was more thinking about
some readymix kinda solution, maybe through API calls, but which?

<http://www.mentalis.org/soft/class.qpx?id=6>
 
R

RDI

'if the key currently is null return "DefaultValue"
sGetINI(sINIFile, "SectionName", "KeyName, "DefaultValue")

sWriteINI(sINIFile, "SectionName", "KeyName, "Vaule")

Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Integer

'Store the location of the INI file
Public Shared Function sGetINI(ByVal sINIFile As String, ByVal sSection As String, ByVal sKey _
As String, ByVal sDefault As String) As String
Dim sTemp As String = Space(255)
Dim nLength As Integer

nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, _
255, sINIFile)
Return sTemp.Substring(0, nLength)
End Function

Public Shared Sub writeINI(ByVal sINIFile As String, ByVal sSection As String, ByVal sKey _
As String, ByVal sValue As String)
'Remove CR/LF characters
sValue = sValue.Replace(vbCr, vbNullChar)
sValue = sValue.Replace(vbLf, vbNullChar)
'Write information to INI file
WritePrivateProfileString(sSection, sKey, sValue, sINIFile)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top