VB.NET 怎么读取和写入INI 文件的?

派大奇 2009-08-13 01:36:05
VB.NET 怎么读取和写入INI 文件的?
...全文
193 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
想飞的狼 2009-08-13
  • 打赏
  • 举报
回复
ding
xml比较流行
Snowdust 2009-08-13
  • 打赏
  • 举报
回复
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 WritePrivateProfileStrin
g Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pFile As String) As Integer
    Private Declare Function WritePrivateProfileStruc
t 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)
      WritePrivateProfileStrin
g(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)
      WritePrivateProfileStrin
g(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)
      WritePrivateProfileStrin
g(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
yanlongwuhui 2009-08-13
  • 打赏
  • 举报
回复
通过API函数WritePrivateProfileString、GetPrivateProfileString等进行读写

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧