| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 运算函数 >> 实例分析正文
最新推荐 更多内容
VB.Net操作INI文件类的实例
VB.Net操作INI文件类的实例
作者:IVA    来源:乐博网     更新时间:2007-12-24

VB.Net操作INI文件类的实例

乐博网将C#代码转化为VB.Net 主体功能完善 个别参数可能需要微调 大家再自己实践

实例代码如下: 

Imports System
Imports System.Text
Imports System.Runtime.InteropServices

Namespace Lob_ini

    Public Class cIni

        Private ls_IniFilename As String

        Private li_BufferLen As Integer = 256

        ''' <summary>
        ''' cINI Constructor
        ''' </summary>
        Public Sub New(ByVal pIniFilename As String)
            MyBase.New()
            ls_IniFilename = pIniFilename
        End Sub

        ''' <summary>
        ''' INI filename (If no path is specifyed the function will look with-in the windows directory for the file)
        ''' </summary>
        Public Property IniFile() As String
            Get
                Return
            End Get
            Set(ByVal value As String)
                ls_IniFilename = value
            End Set
        End Property

        ''' <summary>
        ''' Max return length when reading data (Max: 32767)
        ''' </summary>
        Public Property BufferLen() As Integer
            Get
                Return li_BufferLen
            End Get
            Set(ByVal value As Integer)
                If (value > 32767) Then
                    li_BufferLen = 32767
                ElseIf (value < 1) Then
                    li_BufferLen = 1
                Else
                    li_BufferLen = value
                End If
            End Set
        End Property

        Private Declare Function WritePrivateProfileString Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pFile As String) As Integer

        Private Declare Function WritePrivateProfileStruct Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pValueLen As Integer, ByVal pFile As String) As Integer

        Private Declare Function GetPrivateProfileString Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String, ByVal prReturn() As Byte, ByVal pBufferLen As Integer, ByVal pFile As String) As Integer

        Private Declare Function GetPrivateProfileStruct Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal prReturn() As Byte, ByVal pBufferLen As Integer, ByVal pFile As String) As Integer

        ''' <summary>
        ''' Read value from INI File
        ''' </summary>
        Public Overloads Function ReadValue(ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String) As String
            Return z_GetString(pSection, pKey, pDefault)
        End Function

        ''' <summary>
        ''' Read value from INI File, default = ""
        ''' </summary>
        Public Overloads Function ReadValue(ByVal pSection As String, ByVal pKey As String) As String
            Return z_GetString(pSection, pKey, "")
        End Function

        ''' <summary>
        ''' Write value to INI File
        ''' </summary>
        Public Sub WriteValue(ByVal pSection As String, ByVal pKey As String, ByVal pValue As String)
            WritePrivateProfileString(pSection, pKey, pValue, Me.ls_IniFilename)
        End Sub

        ''' <summary>
        ''' Remove value from INI File
        ''' </summary>
        Public Sub RemoveValue(ByVal pSection As String, ByVal pKey As String)
            WritePrivateProfileString(pSection, pKey, Nothing, Me.ls_IniFilename)
        End Sub

        ''' <summary>
        ''' Read values in a section from INI File
        ''' </summary>
        Public Sub ReadValues(ByVal pSection As String, ByRef pValues As Array)
            pValues = z_GetString(pSection, Nothing, Nothing).Split(CType(ChrW(0), Char))
        End Sub

        ''' <summary>
        ''' Read sections from INI File
        ''' </summary>
        Public Sub ReadSections(ByRef pSections As Array)
            pSections = z_GetString(Nothing, Nothing, Nothing).Split(CType(ChrW(0), Char))
        End Sub

        ''' <summary>
        ''' Remove section from INI File
        ''' </summary>
        Public Sub RemoveSection(ByVal pSection As String)
            WritePrivateProfileString(pSection, Nothing, Nothing, Me.ls_IniFilename)
        End Sub

        ''' <summary>
        ''' Call GetPrivateProfileString / GetPrivateProfileStruct API
        ''' </summary>
        Private Function z_GetString(ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String) As String
            Dim sRet As String = pDefault
            Dim bRet() As Byte = New Byte((li_BufferLen) - 1) {}
            Dim i As Integer = GetPrivateProfileString(pSection, pKey, pDefault, bRet, li_BufferLen, ls_IniFilename)
            sRet = System.Text.Encoding.GetEncoding(1252).GetString(bRet, 0, i).TrimEnd(CType(ChrW(0), Char))
            Return sRet
        End Function
    End Class
End Namespace


 

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    没有相关实例分析

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]