INI file

J

jim

I am converting from VB6 and had previously used
GetPrivateProfileString()' to get values from an INI file.

What is the preferred method for doing this in visual basic dot net??

thanks
 
M

Mr. Arnold

jim said:
I am converting from VB6 and had previously used GetPrivateProfileString()'
to get values from an INI file.

What is the preferred method for doing this in visual basic dot net??

..Net uses the configuration manager.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(VS.80).aspx
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

There are plenty of examples of how to use the Configuration Manager for
appname.exe.config for non Web solutions or Web.config for Web solutions

You should also learn about how to create and use XML files as there is a
whole namespace for that, which you can create your own XML type
configuration files and use the XML namespace too read for data by tag to
get the information from a XML tag in a XML file that can be located
anywhere.

Web.config and appname.exe.config are just XML files. You should find out
how to make a appname.exe.config from within the VS IDE for Visual Basic,
which is done automatically for you by right-clicking the project in the VS
IDE, go to Properties and Settings tab.

You just put something in there a tag and some data like name = Trash,
string type, and enter some data like 'Trash' and watch the app.config
show-up in the project. Also when you compile the program, locate the exe
for the program in the BIN directory where you will find its
appname.exe.config that the exe itself will use to get app.configuration
information from the file.

There is nothing wrong with an .ini file, and you can use a FileReader to
read a text file. But you should think on now switching to XML.

A big key to .NET is do you understand XML, like using an XML serialized
object, even a 'dataset' full of tables and data can be reduced to an XML
serialized object and from the XML serialized object returned to being a
dataset.

In .NET, everything is an 'object'.
 
J

jim

Thanks. Easier than VB6.

Mr. Arnold said:
.Net uses the configuration manager.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(VS.80).aspx

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx


There are plenty of examples of how to use the Configuration Manager for
appname.exe.config for non Web solutions or Web.config for Web solutions

You should also learn about how to create and use XML files as there is
a whole namespace for that, which you can create your own XML type
configuration files and use the XML namespace too read for data by tag
to get the information from a XML tag in a XML file that can be located
anywhere.

Web.config and appname.exe.config are just XML files. You should find
out how to make a appname.exe.config from within the VS IDE for Visual
Basic, which is done automatically for you by right-clicking the project
in the VS IDE, go to Properties and Settings tab.

You just put something in there a tag and some data like name = Trash,
string type, and enter some data like 'Trash' and watch the app.config
show-up in the project. Also when you compile the program, locate the
exe for the program in the BIN directory where you will find its
appname.exe.config that the exe itself will use to get app.configuration
information from the file.

There is nothing wrong with an .ini file, and you can use a FileReader
to read a text file. But you should think on now switching to XML.

A big key to .NET is do you understand XML, like using an XML serialized
object, even a 'dataset' full of tables and data can be reduced to an
XML serialized object and from the XML serialized object returned to
being a dataset.

In .NET, everything is an 'object'.
 
H

Herfried K. Wagner [MVP]

jim said:
I am converting from VB6 and had previously used GetPrivateProfileString()'
to get values from an INI file.

What is the preferred method for doing this in visual basic dot net??

If you need to maintain compatibility to existing INI files, take a look at
this project:

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/IniLib.zip>

Otherwise I suggest to use 'My.Settings'. Note that only user settings can
be written at runtime.
 
B

Bruce W. Roeser

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
 
B

Bruce W. Roeser

Mike,

Nice.

However, the code snippet seems fine except there are some references
missing. In order to resolve them you need to import:

System.Text
System.Collections.Specialized
System.IO

The only one I can't find so far is the TrailSlash() function. Is that a
home-grown one you wrote? Can't find it in a namespace anywhere.

Thanks,

-b :)



Michael D. Ober said:
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.
 
B

Bruce W. Roeser

Mike,

Thanks ... I could just about guess what TrailSlash() was doing from the
context but wanted to be sure. Thanks!

-b :)

Michael D. Ober said:
This is in my library routines, so the three imports needed are part of
the overall file. Also, here's the definition of TrailSlash().

public function TrailSlash(s as string) as string
return s.trimend("\"c) & "\"
end function

Mike.


Bruce W. Roeser said:
Mike,

Nice.

However, the code snippet seems fine except there are some references
missing. In order to resolve them you need to import:

System.Text
System.Collections.Specialized
System.IO

The only one I can't find so far is the TrailSlash() function. Is that a
home-grown one you wrote? Can't find it in a namespace anywhere.

Thanks,

-b :)



Michael D. Ober 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


I am converting from VB6 and had previously used
GetPrivateProfileString()' to get values from an INI file.

What is the preferred method for doing this in visual basic dot net??

thanks


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.
 

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