Imports System
Imports System.Collections
Imports Org.Mentalis.Files
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class TestAppClass TestApp
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Sub Main()Sub Main()
Dim ini As New IniReader("c:\test.ini")
ini.Write("Section1", "KeyString", "MyString")
ini.Write("Section1", "KeyInt", 5)
ini.Write("Section2", "KeyBool", True)
ini.Write("Section2", "KeyBytes", New Byte() {0, 123, 255})
ini.Write("Section3", "KeyLong", CType(123456789101112, Long))
ini.Section = "Section1"
Console.WriteLine("String: " + ini.ReadString("KeyString"))
Console.WriteLine("Int: " + ini.ReadInteger("KeyInt", 0).ToString())
Console.WriteLine("Bool: " + ini.ReadBoolean("Section2", "KeyBool", False).ToString())
Console.WriteLine("Long: " + ini.ReadLong("Section3", "KeyLong", 0).ToString())
Console.WriteLine("Byte 1 in byte array: " + (ini.ReadByteArray("Section2", "KeyBytes")(1)).ToString())
ini.DeleteKey("Section2", "KeyBytes")
ini.DeleteSection("Section3")
Dim e As IEnumerator = ini.GetSectionNames().GetEnumerator()
While e.MoveNext()
Console.WriteLine(e.Current)
End While
Console.WriteLine("Press enter to continue
")
Console.ReadLine()
End Sub
End Class
Imports System
Imports System.Text
Imports System.Collections
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class IniReaderClass IniReader
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Declare Ansi Function GetPrivateProfileInt()Function GetPrivateProfileInt Lib "kernel32.dll" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Declare Ansi Function WritePrivateProfileString()Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Declare Ansi Function GetPrivateProfileString()Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Declare Ansi Function GetPrivateProfileSectionNames()Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer() As Byte, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Declare Ansi Function WritePrivateProfileSection()Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New(ByVal file As String)
Filename = file
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Property Filename()Property Filename() As String
Get
Return m_Filename
End Get
Set(ByVal Value As String)
m_Filename = Value
End Set
End Property
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Property Section()Property Section() As String
Get
Return m_Section
End Get
Set(ByVal Value As String)
m_Section = Value
End Set
End Property
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadInteger()Function ReadInteger(ByVal section As String, ByVal key As String, ByVal defVal As Integer) As Integer
Return GetPrivateProfileInt(section, key, defVal, Filename)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadInteger()Function ReadInteger(ByVal section As String, ByVal key As String) As Integer
Return ReadInteger(section, key, 0)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadInteger()Function ReadInteger(ByVal key As String, ByVal defVal As Integer) As Integer
Return ReadInteger(Section, key, defVal)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadInteger()Function ReadInteger(ByVal key As String) As Integer
Return ReadInteger(key, 0)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadString()Function ReadString(ByVal section As String, ByVal key As String, ByVal defVal As String) As String
Dim sb As New StringBuilder(MAX_ENTRY)
Dim Ret As Integer = GetPrivateProfileString(section, key, defVal, sb, MAX_ENTRY, Filename)
Return sb.ToString()
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadString()Function ReadString(ByVal section As String, ByVal key As String) As String
Return ReadString(section, key, "")
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadString()Function ReadString(ByVal key As String) As String
Return ReadString(Section, key)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadLong()Function ReadLong(ByVal section As String, ByVal key As String, ByVal defVal As Long) As Long
Return Long.Parse(ReadString(section, key, defVal.ToString()))
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadLong()Function ReadLong(ByVal section As String, ByVal key As String) As Long
Return ReadLong(section, key, 0)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadLong()Function ReadLong(ByVal key As String, ByVal defVal As Long) As Long
Return ReadLong(Section, key, defVal)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadLong()Function ReadLong(ByVal key As String) As Long
Return ReadLong(key, 0)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadByteArray()Function ReadByteArray(ByVal section As String, ByVal key As String) As Byte()
Try
Return Convert.FromBase64String(ReadString(section, key))
Catch
End Try
Return Nothing
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadByteArray()Function ReadByteArray(ByVal key As String) As Byte()
Return ReadByteArray(Section, key)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadBoolean()Function ReadBoolean(ByVal section As String, ByVal key As String, ByVal defVal As Boolean) As Boolean
Return Boolean.Parse(ReadString(section, key, defVal.ToString()))
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadBoolean()Function ReadBoolean(ByVal section As String, ByVal key As String) As Boolean
Return ReadBoolean(section, key, False)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadBoolean()Function ReadBoolean(ByVal key As String, ByVal defVal As Boolean) As Boolean
Return ReadBoolean(Section, key, defVal)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function ReadBoolean()Function ReadBoolean(ByVal key As String) As Boolean
Return ReadBoolean(Section, key)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal section As String, ByVal key As String, ByVal value As Integer) As Boolean
Return Write(Section, key, value.ToString())
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal key As String, ByVal value As Integer) As Boolean
Return Write(Section, key, value)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal section As String, ByVal key As String, ByVal value As String) As Boolean
Return (WritePrivateProfileString(section, key, value, Filename) <> 0)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal key As String, ByVal value As String) As Boolean
Return Write(Section, key, value)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal section As String, ByVal key As String, ByVal value As Long) As Boolean
Return Write(section, key, value.ToString())
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal key As String, ByVal value As Long) As Boolean
Return Write(Section, key, value)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal section As String, ByVal key As String, ByVal value() As Byte) As Boolean
If value Is Nothing Then
Return Write(section, key, CType(Nothing, String))
Else
Return Write(section, key, value, 0, value.Length)
End If
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal key As String, ByVal value() As Byte) As Boolean
Return Write(Section, key, value)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal section As String, ByVal key As String, ByVal value() As Byte, ByVal offset As Integer, ByVal length As Integer) As Boolean
If value Is Nothing Then
Return Write(section, key, CType(Nothing, String))
Else
Return Write(section, key, Convert.ToBase64String(value, offset, length))
End If
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal section As String, ByVal key As String, ByVal value As Boolean) As Boolean
Return Write(section, key, value.ToString())
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function Write()Function Write(ByVal key As String, ByVal value As Boolean) As Boolean
Return Write(Section, key, value)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function DeleteKey()Function DeleteKey(ByVal section As String, ByVal key As String) As Boolean
Return (WritePrivateProfileString(section, key, Nothing, Filename) <> 0)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function DeleteKey()Function DeleteKey(ByVal key As String) As Boolean
Return (WritePrivateProfileString(Section, key, Nothing, Filename) <> 0)
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function DeleteSection()Function DeleteSection(ByVal section As String) As Boolean
Return WritePrivateProfileSection(section, Nothing, Filename) <> 0
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function GetSectionNames()Function GetSectionNames() As ArrayList
Try
Dim buffer(MAX_ENTRY) As Byte
GetPrivateProfileSectionNames(buffer, MAX_ENTRY, Filename)
Dim parts() As String = Encoding.ASCII.GetString(buffer).Trim(ControlChars.NullChar).Split(ControlChars.NullChar)
Return New ArrayList(parts)
Catch
End Try
Return Nothing
End Function
Private m_Filename As String
Private m_Section As String
Private Const MAX_ENTRY As Integer = 32768
End Class
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2007-07-10 01:02
yongwnet
阅读(
636)
评论()
编辑
收藏
举报