Bruce W. Roeser said:
Jim,
GetPrivateProfileString() is in the Windows API and you can still call it
in practically the identical fashion you did under VB6. Just declare the
function similar to how you did it in VB6, you will still be able to call
it. Check the MSDN on how to call Windows API functions in VB.Net, it's
not much different than it was in VB6. I was just doing the same thing
with another API call and it worked fine.
-b
Jim,
Here's a complete INI file wrapper class. I found the basis for this
on-line and fleshed it out as needed. If you don't specify the actual
file location with either an absolute or relative path, it first checks
the application directory for the file and then checks the Windows
directory %windir%.
Be aware that dotNET purists will tell you to either use .config or .XML
files, but if you are like me and still have a large base of VB6 and VC6
code to support, INI files are still the only way to go.
Mike.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' To make the code easier to read, wrap the INI file interface in a
public nested class
<System.Runtime.InteropServices.ComVisible(False)> Public Class
iniWrapper
#Region "INI File Support API"
Private Declare Ansi Function GetPrivateProfileString Lib
"KERNEL32.DLL" Alias "GetPrivateProfileStringA" _
(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
Private Declare Ansi Function GetPrivateProfileInt Lib "KERNEL32.DLL"
Alias "GetPrivateProfileIntA" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal iDefault As Integer, _
ByVal lpFileName As String) As Integer
Private Declare Ansi Function WritePrivateProfileString Lib
"KERNEL32.DLL" Alias "WritePrivateProfileStringA" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Boolean
Private Declare Ansi Function GetPrivateProfileSection Lib
"KERNEL32.DLL" Alias "GetPrivateProfileSectionA" _
(ByVal lpAppName As String, _
ByVal lpReturnedString As Byte(), _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
Private Declare Ansi Function WritePrivateProfileSection Lib
"KERNEL32.DLL" Alias "WritePrivateProfileSectionA" _
(ByVal lpAppName As String, _
ByVal data As Byte(), _
ByVal lpFileName As String) As Boolean
Private Declare Ansi Function GetPrivateProfileSectionNames Lib
"KERNEL32.DLL" Alias "GetPrivateProfileSectionNamesA" _
(ByVal lpReturnedString As Byte(), _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
#End Region
#Region "INI File Strings"
Public Shared Function ReadString(ByVal Section As String, ByVal Key As
String, ByVal iniFile As String, Optional ByVal [Default] As String = "")
As String
Dim buffer As StringBuilder
Dim nSize As Integer = Math.Max(100, Len([Default]) + 2)
Dim retSize As Integer
iniFile = FindINIFile(iniFile)
Do
buffer = New StringBuilder(nSize)
retSize = GetPrivateProfileString(Section, Key, [Default], buffer,
buffer.Capacity, iniFile)
If retSize < nSize Then Exit Do
nSize = retSize + 2
Loop
Return
System.Environment.ExpandEnvironmentVariables(buffer.ToString())
End Function
Public Shared Function WriteString(ByVal Section As String, ByVal Key
As String, ByVal iniFile As String, ByVal Value As String) As Boolean
Return WritePrivateProfileString(Section, Key, Value,
FindINIFile(iniFile))
End Function
#End Region
#Region "INI File Integers"
Public Shared Function ReadInt(ByVal Section As String, ByVal Key As
String, ByVal iniFile As String, Optional ByVal [Default] As Integer = 0)
As Integer
Return GetPrivateProfileInt(Section, Key, [Default],
FindINIFile(iniFile))
End Function
Public Shared Function WriteInt(ByVal Section As String, ByVal Key As
String, ByVal iniFile As String, ByVal Value As Integer) As Boolean
Return WriteString(Section, Key, iniFile, Value.ToString)
End Function
#End Region
#Region "INI File Booleans"
Public Shared Function ReadBool(ByVal Section As String, ByVal Key As
String, ByVal iniFile As String, Optional ByVal bDefault As Boolean =
False) As Boolean
Return CBool(ReadInt(Section, Key, iniFile, CInt(bDefault)))
End Function
Public Shared Function WriteBool(ByVal Section As String, ByVal Key As
String, ByVal iniFile As String, ByVal Value As Boolean) As Boolean
Return WriteInt(Section, Key, iniFile, CInt(Value))
End Function
#End Region
#Region "INI File Sections"
Private Const MaxSectionSize As Integer = CInt(2 ^ 15 - 1)
Public Shared Function GetSection(ByVal Section As String, ByVal
iniFile As String) As StringCollection
Dim items As StringCollection = New StringCollection()
Dim buffer(MaxSectionSize) As Byte ' INI Files are limited to 32Kb
in size
Dim bufLen As Integer = 0
Dim sb As StringBuilder
Dim i As Integer
bufLen = GetPrivateProfileSection(Section, buffer,
buffer.GetUpperBound(0), FindINIFile(iniFile))
If bufLen > 0 Then
sb = New StringBuilder()
For i = 0 To bufLen - 1
If buffer(i) <> 0 Then
sb.Append(ChrW(buffer(i)))
Else
If sb.Length > 0 Then
items.Add(sb.ToString())
sb = New StringBuilder()
End If
End If
Next
End If
Return items
End Function
Public Shared Function WriteSection(ByVal Section As String, ByVal
Items As StringCollection, ByVal iniFile As String) As Boolean
Dim b(MaxSectionSize) As Byte
Dim j As Integer = 0
Dim s As String
For Each s In Items
ASCIIEncoding.ASCII.GetBytes(s, 0, s.Length, b, j)
j += s.Length
b(j) = 0
j += 1
Next
b(j) = 0
Return WritePrivateProfileSection(Section, b, FindINIFile(iniFile))
End Function
Public Shared Function GetSectionNames(ByVal FileName As String) As
StringCollection
Dim sections As StringCollection = New StringCollection()
Dim buffer(MaxSectionSize) As Byte
Dim bufLen As Integer = 0
Dim sb As StringBuilder
Dim i As Integer
bufLen = GetPrivateProfileSectionNames(buffer,
buffer.GetUpperBound(0), FindINIFile(FileName))
If bufLen > 0 Then
sb = New StringBuilder()
For i = 0 To bufLen - 1
If buffer(i) <> 0 Then
sb.Append(ChrW(buffer(i)))
Else
If sb.Length > 0 Then
sections.Add(sb.ToString())
sb = New StringBuilder()
End If
End If
Next
End If
Return sections
End Function
#End Region
#Region "INI File Support"
Private Shared Function FindINIFile(ByVal iniFile As String) As String
If Path.GetExtension(iniFile) <> ".ini" Then
Dim t As String = Path.GetDirectoryName(iniFile)
If t <> "" Then t = TrailSlash(t)
iniFile = t & Path.GetFileNameWithoutExtension(iniFile) & ".ini"
End If
If iniFile.Contains("\") Then Return iniFile
If File.Exists(TrailSlash(My.Application.Info.DirectoryPath()) &
iniFile) Then
Return TrailSlash(My.Application.Info.DirectoryPath()) & iniFile
End If
Return iniFile
End Function
#End Region
End Class 'iniWrapper
' Mike.