> How do i read and write ini file using vb.net?
I see a LOT of searching has been done!!!
(Hint - Use Google and also this is a VB group, NOT a vb.net group, try a
group with dotnet in its title)
hi TBoon,
An ini file is a text file, so you use ordinary vb
read/write statements.
There are a few examples of "class code" for dealing
with ini files, but generally speaking the format of
an ini file is so easy that you are probably better
off "rolling-yer-own".
If you are looking for api calls specifically oriented
to ini files, I don't know of any.
cheers, jw
> How do i read and write ini file using vb.net?
Matt Williamson - 21 Dec 2006 23:17 GMT
> If you are looking for api calls specifically oriented
> to ini files, I don't know of any.
GetPrivateProfileString and WritePrivateProfileString but I don't know how
that applies to vFred.
This is all you may need.
I don't think you should read or write to .ini files with the normal file
I/O
as that can cause problems due to the Windows cache.
Option Explicit
Private Declare Function _
GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName _
As String) As Long
Private Declare Function _
WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Long
Function ReadINIValue(ByVal strINIPath As String, _
ByVal strSection As String, _
ByVal strKey As String) As String
'will return <no value> if the header or the key is not there
'will return <no file> if the .ini file is not there
'------------------------------------------------------------
Dim buf As String * 256
Dim Length As Long
If bFileExists(strINIPath) = False Then
ReadINIValue = "<no file>"
Exit Function
End If
Length = GetPrivateProfileString(strSection, _
strKey, _
"<no value>", _
buf, _
Len(buf), _
strINIPath)
ReadINIValue = Left$(buf, Length)
End Function
Sub DeleteIniKey(ByVal strINIPath As String, _
ByVal strSection As String, _
ByVal strKey As String)
On Error Resume Next
WritePrivateProfileString strSection, _
strKey, _
vbNullString, _
strINIPath
On Error GoTo 0
End Sub
Function WriteIniValue(ByVal strINIPath As String, _
ByVal strSection As String, _
ByVal strKey As String, _
ByVal strValue As String) As Boolean
If bFileExists(strINIPath) = False Then
WriteIniValue = False
Exit Function
End If
WritePrivateProfileString strSection, _
strKey, _
strValue, _
strINIPath
WriteIniValue = True
End Function
Function bFileExists(ByVal strFile As String) As Boolean
Dim lAttr As Long
On Error Resume Next
lAttr = GetAttr(strFile)
bFileExists = (Err.Number = 0) And ((lAttr And vbDirectory) = 0)
On Error GoTo 0
End Function
RBS
> How do i read and write ini file using vb.net?
RB Smissaert - 22 Dec 2006 13:57 GMT
Ah, noticed you are in .net.
Won't work in that case.
RBS
> This is all you may need.
> I don't think you should read or write to .ini files with the normal file
[quoted text clipped - 92 lines]
>
>> How do i read and write ini file using vb.net?
J French - 23 Dec 2006 12:33 GMT
>This is all you may need.
>I don't think you should read or write to .ini files with the normal file
>I/O
>as that can cause problems due to the Windows cache.
There is no problem if your App(s) are the only one(s) that use the
INI file - although that presupposes write access to the directory.