田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 3 48
     12
3456789
10111213141516
17181920212223
24252627282930
31


站点统计

最新评论



隐藏日志,无权浏览 注册表保存数据
未知 TcINIFile   [ 日期:2018-12-13 ]   [ 来自:本站原创 ]  HTML
程序代码:

Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text

Namespace TcINIFile
    Public Class INIFile
        Private _FileName As String

        Private Declare Function GetPrivateProfileStringA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal keyName As String, ByVal sDefault As String, ByVal buffer As StringBuilder, ByVal nSize As Integer, ByVal fileName As String) As Integer

        Private Declare Function GetPrivateProfileSectionA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal buffer As StringBuilder, ByVal nSize As Integer, ByVal fileName As String) As Integer

        Private Declare Function WritePrivateProfileSectionA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal sValue As String, ByVal fileName As String) As Integer

        Private Declare Function WritePrivateProfileStringA Lib "kernel32.dll" _
            (ByVal segName As String, ByVal keyName As String, ByVal sValue As String, ByVal fileName As String) As Integer

        Private Declare Function GetPrivateProfileSectionNamesA Lib "kernel32.dll" _
            (ByVal buffer As Byte(), ByVal iLen As Integer, ByVal fileName As String) As Integer

        Private Declare Function GetPrivateProfileString Lib "kernel32" _
            (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As Byte(), ByVal nSize As UInteger, ByVal lpFileName As String) As UInteger

        Public Sub New(ByVal FileName As String)
            Me._FileName = FileName
            If Not Me.FileExists() Then
                Me.CreateFile()
            End If
        End Sub

        Public Function ReadString(ByVal Section As String, ByVal Key As String) As String
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileStringA(Section, Key, "", stringBuilder, stringBuilder.Capacity, Me._FileName)
            Return stringBuilder.ToString()
        End Function

        Public Function ReadList(ByVal Section As String, ByVal Key As String) As List(Of String)
            Dim list As List(Of String) = New List(Of String)()
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileStringA(Section, Key, "", stringBuilder, stringBuilder.Capacity, Me._FileName)
            For i As Integer = 0 To stringBuilder.ToString().Split(",").Length - 1
                list.Add(stringBuilder.ToString().Split(",")(i).ToString())
            Next
            Return list
        End Function

        Public Function ReadList2(ByVal Section As String, ByVal Key As String) As List(Of String)
            Dim list As List(Of String) = New List(Of String)()
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileStringA(Section, Key, "", stringBuilder, stringBuilder.Capacity, Me._FileName)
            For i As Integer = 0 To stringBuilder.ToString().Split(vbLf).Length - 1
                list.Add(stringBuilder.ToString().Split(vbLf)(i).ToString())
            Next
            Return list
        End Function

        Public Function ReadSingleSectionKeys(ByVal Section As String) As List(Of String)
            Dim list As List(Of String) = New List(Of String)()
            Dim array As Byte() = New Byte(65536 - 1) {}
            Dim privateProfileString As UInteger = INIFile.GetPrivateProfileString(Section, Nothing, Nothing, array, CUInt(array.Length), Me._FileName)
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            While CLng(num2) < CLng((CULng(privateProfileString)))
                If array(num2) = 0 Then
                    list.Add(Encoding.[Default].GetString(array, num, num2 - num))
                    num = num2 + 1
                End If
                num2 += 1
            End While
            Return list
        End Function

        Public Overridable Function ReadInt(ByVal Section As String, ByVal Key As String) As Integer
            Dim result As Integer
            Try
                result = Integer.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1
            End Try
            Return result
        End Function

        Public Overridable Function ReadLong(ByVal Section As String, ByVal Key As String) As Long
            Dim result As Long
            Try
                result = Long.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1L
            End Try
            Return result
        End Function

        Public Overridable Function ReadByte(ByVal Section As String, ByVal Key As String) As Byte
            Dim result As Byte
            Try
                result = Byte.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = 0
            End Try
            Return result
        End Function

        Public Overridable Function ReadFloat(ByVal Section As String, ByVal Key As String) As Single
            Dim result As Single
            Try
                result = Single.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1.0F
            End Try
            Return result
        End Function

        Public Overridable Function ReadDouble(ByVal Section As String, ByVal Key As String) As Double
            Dim result As Double
            Try
                result = Double.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = -1.0
            End Try
            Return result
        End Function

        Public Overridable Function ReadDateTime(ByVal Section As String, ByVal Key As String) As DateTime
            Dim result As DateTime
            Try
                result = DateTime.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = DateTime.Parse("0-0-0")
            End Try
            Return result
        End Function

        Public Overridable Function ReadBool(ByVal Section As String, ByVal Key As String) As Boolean
            Dim result As Boolean
            Try
                result = Boolean.Parse(Me.ReadString(Section, Key))
            Catch ex_13 As Exception
                result = Boolean.Parse("0-0-0")
            End Try
            Return result
        End Function

        Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Object)
            If Value IsNot Nothing Then
                INIFile.WritePrivateProfileStringA(Section, Key, Value.ToString(), Me._FileName)
            Else
                INIFile.WritePrivateProfileStringA(Section, Key, Nothing, Me._FileName)
            End If
        End Sub

        Public Function ReadSections() As ArrayList
            Dim array As Byte() = New Byte(65535 - 1) {}
            Dim privateProfileSectionNamesA As Integer = INIFile.GetPrivateProfileSectionNamesA(array, array.GetUpperBound(0), Me._FileName)
            Dim arrayList As ArrayList = New ArrayList()
            If privateProfileSectionNamesA > 0 Then
                Dim index As Integer = 0
                For i As Integer = 0 To privateProfileSectionNamesA - 1
                    If array(i) = 0 Then
                        Dim text As String = Encoding.[Default].GetString(array, index, i).Trim()
                        index = i + 1
                        If text <> "" Then
                            arrayList.Add(text)
                        End If
                    End If
                Next
            End If
            Return arrayList
        End Function

        Public Function SectionExists(ByVal Section As String) As Boolean
            Dim stringBuilder As StringBuilder = New StringBuilder(65535)
            INIFile.GetPrivateProfileSectionA(Section, stringBuilder, stringBuilder.Capacity, Me._FileName)
            Return Not (stringBuilder.ToString().Trim() = "")
        End Function

        Public Function ValueExits(ByVal Section As String, ByVal Key As String) As Boolean
            Return Not (Me.ReadString(Section, Key).Trim() = "")
        End Function

        Public Sub DeleteKey(ByVal Section As String, ByVal Key As String)
            Me.Write(Section, Key, Nothing)
        End Sub

        Public Sub DeleteSection(ByVal Section As String)
            INIFile.WritePrivateProfileSectionA(Section, Nothing, Me._FileName)
        End Sub

        Public Sub AddSection(ByVal Section As String)
            INIFile.WritePrivateProfileSectionA(Section, "", Me._FileName)
        End Sub

        Public Sub DeleteFile()
            If Me.FileExists() Then
                File.Delete(Me._FileName)
            End If
        End Sub

        Public Sub CreateFile()
            File.Create(Me._FileName).Close()
        End Sub

        Public Function FileExists() As Boolean
            Return File.Exists(Me._FileName)
        End Function
    End Class
End Namespace





暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:1*6=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©