📄 ini.vb
字号:
'==========================================================================
'
' File: INI.vb
' Location: FileSystem <Visual Basic .Net>
' Description: INI控制类及相关
' Created: 2004.10.31.09:33:47(GMT+8:00)
' Version: 0.5 2007.04.29.
' Copyright(C) F.R.C.
'
'==========================================================================
Option Compare Text
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.ControlChars
Imports System
Imports System.Collections
Imports System.IO
Imports System.Text
''' <summary>INI控制类</summary>
''' <remarks>
''' 本类管理INI文件
''' 注意 本类初始化时会从文件读取数据,但没有文件也可
''' 注意 相同的键只保留后者
''' 注意 写函数
'''
''' 注意 本类的字符串支持字符转义
''' @ 在字符串前可取消字符转义
''' { } 可用表示多行文字 此时自动禁止转义 {必须在有等号那行 }必须是那行的最后一个除空格以外的字符
''' $ 在字符串前表示字符串的值从后面的外部文件得到 此时自动禁止转义
''' 若@{$连用只有前面的起作用
''' ; # // 用作单行注释
''' /* */ 用作多行注释
''' 没有等号和节格式的行不处理 不推荐作为注释
''' \a 与响铃(警报)\u0007 匹配
''' \b 与退格符 \u0008 匹配
''' \t 与 Tab 符 \u0009 匹配
''' \r 与回车符 \u000D 匹配
''' \v 与垂直 Tab 符 \u000B 匹配
''' \f 与换页符 \u000C 匹配
''' \n 与换行符 \u000A 匹配
''' \e 与 Esc 符 \u001B 匹配
''' \x?? 与 Chr(??) 匹配
''' \x2F 与 左斜杠符 / 匹配
''' \x5C 与 右斜杠符 \ 匹配
'''
''' 本类使用ReadValue来读值 如果没有读出返回False 用New INI(FILE_NAME)得到的实例会自动调用此函数
''' 本类使用WriteValue来写入值到内存
''' 本类使用ReadFromFile将从文件添入值 如果没有文件可用返回False
''' 本类使用WriteToFile将所有改变写入文件 如果没有写入返回False
''' </remarks>
Public Class INI
Private FileName As String
Private Root As New Hashtable
Private Section As String() = New String() {}
Private NoneNameSection As New SectionBase '为null节提供集合
Private Class SectionBase
Public Section As New Hashtable
Public Key() As String
End Class
Public Sub New(ByVal FILE_NAME As String, Optional ByVal Read As Boolean = True)
FileName = FILE_NAME
If Read Then ReadFromFile(FILE_NAME)
End Sub
Public Sub New()
End Sub
Public Function ReadFromFile(ByVal FILE_NAME As String) As Boolean
Dim sr As StreamReader
Dim Line() As String
Try
sr = New StreamReader(FILE_NAME, System.Text.Encoding.Default)
Line = sr.ReadToEnd.Replace(Environment.NewLine, ChrW(10)).Split(ChrW(10))
sr.Close()
Catch
Return False
End Try
Dim TempLine() As String
Dim TempSectionName As String = ""
For n As Integer = 0 To Line.GetUpperBound(0)
'处理节和键
TempLine = Line(n).Split(New Char() {"="}, 2)
For m As Integer = 0 To TempLine.GetUpperBound(0)
TempLine(m) = TempLine(m).Trim(" ")
Next
If TempLine.GetLength(0) = 1 Then
' 处理节
If TempLine(0).StartsWith("[") AndAlso TempLine(0).EndsWith("]") Then
TempSectionName = TempLine(0).TrimStart("[").TrimEnd("]")
If TempSectionName <> "" AndAlso Not Root.Contains(TempSectionName) Then
ReDim Preserve Section(Section.GetUpperBound(0) + 1)
Section(Section.GetUpperBound(0)) = TempSectionName
Root.Add(TempSectionName, New SectionBase)
End If
End If
Else
If TempLine(0) = "" Then Continue For
If TempLine(0).Contains(";") Then Continue For
If TempLine(0).Contains("#") Then Continue For
If TempLine(0).Contains("//") Then Continue For
If TempLine(0).Contains("/*") Then Continue For
If TempLine(0).Contains("*/") Then Continue For
If TempLine(1).StartsWith("{") Then
Dim s As String = TempLine(1).Remove(0, 1)
If s <> "" Then s = s & Environment.NewLine
While Not Line(n).EndsWith("}")
n += 1
If n > Line.GetUpperBound(0) Then Exit While
s &= Line(n) & Environment.NewLine
End While
If s.EndsWith("}" & Environment.NewLine) Then s = s.Substring(0, s.Length - 1 - Environment.NewLine.Length)
If s.EndsWith(Environment.NewLine) Then s = s.Substring(0, s.Length - Environment.NewLine.Length)
TempLine(1) = "@" & s
ElseIf TempLine(1).StartsWith("$") Then
Try
sr = New StreamReader(TempLine(1).Remove(0, 1), System.Text.Encoding.Default)
TempLine(1) = sr.ReadToEnd
sr.Close()
Catch
Continue For
End Try
Else
'除去"/*"到"*/"的注释
Dim Index As Integer = TempLine(1).IndexOf("/*")
If Not Index < 0 Then
Dim Index2 As Integer = TempLine(1).IndexOf("*/")
If Index2 > Index Then
TempLine(1) = TempLine(1).Substring(0, Index - 1 + 1) & TempLine(1).Substring(Index2 + 2)
Else
TempLine(1) = TempLine(1).Substring(0, Index - 1 + 1)
n += 1
While Line(n).IndexOf("*/") < 0
n += 1
If n > Line.GetUpperBound(0) Then Exit For
End While
TempLine(1) &= Line(n).Substring(Line(n).IndexOf("*/") + 2)
End If
End If
'除去单行注释
TempLine(1) = TempLine(1).Split(";")(0)
TempLine(1) = TempLine(1).Split("#")(0)
Index = TempLine(1).IndexOf("//")
If Not (Index < 0) Then TempLine(1) = TempLine(1).Substring(0, Index)
End If
TempLine(1) = TempLine(1).Trim(" ")
'处理键
Dim TheCol As SectionBase
If TempSectionName = "" Then
TheCol = NoneNameSection
Else
TheCol = Root.Item(TempSectionName)
End If
Dim KeyAdd As Boolean
KeyAdd = (TheCol.Key Is Nothing) OrElse (Not TheCol.Section.Contains(TempLine(0)))
If KeyAdd = False Then
TheCol.Section.Remove(TempLine(0))
Else
If Not TheCol.Key Is Nothing Then
ReDim Preserve TheCol.Key(TheCol.Key.GetUpperBound(0) + 1)
Else
ReDim TheCol.Key(0)
End If
TheCol.Key(TheCol.Key.GetUpperBound(0)) = TempLine(0)
End If
TheCol.Section.Add(TempLine(0), TempLine(1))
End If
Next
End Function
Public Function ReadValue(ByVal SectionToRead As String, ByVal Key As String, ByRef Value As Boolean) As Boolean
'读数据
Dim TheCol As SectionBase
If SectionToRead = "" Then
TheCol = NoneNameSection
Else
If Root.Contains(SectionToRead) Then
TheCol = Root.Item(SectionToRead)
Else
Return False
End If
End If
Dim TempValue As String
If (Key <> "") AndAlso (TheCol.Section.Contains(Key)) Then
Try
TempValue = TheCol.Section.Item(Key)
Catch
Return False
End Try
Else
Return False
End If
If TempValue = "False" Then
Value = False
ElseIf TempValue = "True" Then
Value = True
End If
End Function
Public Function ReadValue(ByVal SectionToRead As String, ByVal Key As String, ByRef Value As Byte) As Boolean
'读数据
Dim TheCol As SectionBase
If SectionToRead = "" Then
TheCol = NoneNameSection
Else
If Root.Contains(SectionToRead) Then
TheCol = Root.Item(SectionToRead)
Else
Return False
End If
End If
Dim TempValue As Integer
If (Key <> "") AndAlso (TheCol.Section.Contains(Key)) Then
Try
TempValue = TheCol.Section.Item(Key)
Catch
Return False
End Try
Else
Return False
End If
If Not TempValue.ToString = TheCol.Section.Item(Key) Then Return False
If TempValue < 0 OrElse TempValue > 255 Then Return False
Value = CByte(TempValue)
End Function
Public Function ReadValue(ByVal SectionToRead As String, ByVal Key As String, ByRef Value As String) As Boolean
'读数据
Dim TheCol As SectionBase
If SectionToRead = "" Then
TheCol = NoneNameSection
Else
If Root.Contains(SectionToRead) Then
TheCol = Root.Item(SectionToRead)
Else
Return False
End If
End If
Dim TempString As String
Dim Index As Integer
If (Key <> "") AndAlso (TheCol.Section.Contains(Key)) Then
Try
TempString = TheCol.Section.Item(Key)
Catch
Return False
End Try
Else
Return False
End If
If Not TempString.StartsWith("@") Then
TempString = TempString.Replace("\a", Chr(7))
TempString = TempString.Replace("\b", Back)
TempString = TempString.Replace("\t", Tab)
TempString = TempString.Replace("\r", Cr)
TempString = TempString.Replace("\v", VerticalTab)
TempString = TempString.Replace("\f", Chr(12))
TempString = TempString.Replace("\n", Lf)
TempString = TempString.Replace("\e", Chr(27))
TempString = TempString.Replace("\b", Back)
Index = TempString.IndexOf("\x")
While Not (Index < 0 OrElse Index > TempString.Length - 3)
Dim TempSubstring As String = TempString.Substring(Index + 2, 2)
TempString = TempString.Replace("\x" & TempSubstring, Chr(CInt("&H" & TempSubstring)))
Index = TempString.IndexOf("\x")
End While
Value = TempString
Else
Value = TempString.Remove(0, 1)
End If
Return True
End Function
Public Function ReadValue(Of T)(ByVal SectionToRead As String, ByVal Key As String, ByRef Value As T) As Boolean
'读数据
Dim TheCol As SectionBase
If SectionToRead = "" Then
TheCol = NoneNameSection
Else
If Root.Contains(SectionToRead) Then
TheCol = Root.Item(SectionToRead)
Else
Return False
End If
End If
Dim TempValue As T
If (Key <> "") AndAlso (TheCol.Section.Contains(Key)) Then
Try
TempValue = TheCol.Section.Item(Key)
Catch
Return False
End Try
Else
Return False
End If
Value = TempValue
Return True
End Function
Public Function ReadValue(Of T As IComparable)(ByVal SectionToRead As String, ByVal Key As String, ByRef Value As T, ByVal LowerBound As T, ByVal UpperBound As T) As Boolean
'读数据 不比较LowerBound和UpperBound的大小
Dim TheCol As SectionBase
If SectionToRead = "" Then
TheCol = NoneNameSection
Else
If Root.Contains(SectionToRead) Then
TheCol = Root.Item(SectionToRead)
Else
Return False
End If
End If
Dim TempValue As T
If (Key <> "") AndAlso (TheCol.Section.Contains(Key)) Then
Try
TempValue = TheCol.Section.Item(Key)
Catch
Return False
End Try
Else
Return False
End If
If TempValue.CompareTo(LowerBound) < 0 Then
TempValue = LowerBound
ElseIf TempValue.CompareTo(UpperBound) > 0 Then
TempValue = UpperBound
End If
Value = TempValue
Return True
End Function
Public Function ReadValue(ByVal SectionToRead As String, ByVal Key As String, ByRef Value As String()) As Boolean
'读数据
Dim TheCol As SectionBase
If SectionToRead = "" Then
TheCol = NoneNameSection
Else
If Root.Contains(SectionToRead) Then
TheCol = Root.Item(SectionToRead)
Else
Return False
End If
End If
Dim TempString As String
Dim Index As Integer
If (Key <> "") AndAlso (TheCol.Section.Contains(Key)) Then
Try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -