How do i read and write ini file using vb.net?

  • Thread starter Thread starter TBoon
  • Start date Start date
If you are looking for the legacy approach... (pinvoke.net is a nice site
for finding these sorts of things).

Class Util
Public Shared Function readINI(ByVal sSection As String, ByVal sKey As
String, ByVal sFile As String) As String
Dim sb As StringBuilder
Dim iRet As Integer

sb = New StringBuilder(500)

iRet = WinApi.GetPrivateProfileString(sSection, sKey, "", sb,
sb.Capacity, sFile)

Return sb.ToString
End Function

Public Shared Sub writeINI(ByVal sSection As String, ByVal sKey As String,
ByVal sFile As String, ByVal sValue As String)
Dim bOk As Boolean

bOk = WinApi.WritePrivateProfileString(sSection, sKey, sValue, sFile)
End Sub
end class




Class WinApi
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function GetPrivateProfileString(ByVal lpAppName As String,
_
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
End Function

<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function WritePrivateProfileString(ByVal lpAppName As
String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Boolean
End Function
End Class
 
Back
Top