一个VB读写ini文件问题

一个VB读写ini文件问题,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

刚学读init文件,窗体一个Command按钮,点击他时显示ini文件中对应项的值。代码如下:窗体中

Private Sub Command1_Click()

Dim length

Dim lpFileName

Dim Temp As String * 255

lpFileName = App.Path &"\myIni.ini"

length = GetPrivateProfileString("System", "aa", "", Temp, Len(Temp), lpFileName)

MsgBox Left(Temp, length)

End Sub

模块中

Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As Any, _

ByVal lpString As Any, _

ByVal lpFileName As String) As Long

Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As Any, _

ByVal lpDefault As String, _

ByVal lpReturnedString As String, _

ByVal nSize As Long, _

ByVal lpFileName As String) As Long

为什么运行时只显示一个OK对话框?

解析:

Option Explicit

Public sIniFileDir As String 'ini文件的路径

Public sNeirong() As String

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As Any, _

ByVal lpDefault As String, _

ByVal lpReturnedString As String, _

ByVal nSize As Long, _

ByVal lpFileName As String) As Long

Public Function FlGetUserINI()

Dim lpOut As String * 128

Dim Ret As Long

Dim i As Integer

Dim sUser As String

Dim sKey As String

sIniFileDir = Trim(App.Path)

If Right(sIniFileDir, 1) <>"\" Then sIniFileDir = sIniFileDir &"\"

sIniFileDir = sIniFileDir &"HAS_SIM.INI"

For i = 0 To 1000

sKey = "KEY" &i + 1

Ret = GetPrivateProfileString("keshi", sKey, "", lpOut, 128, sIniFileDir)

sUser = FlLeftB(lpOut, Ret)

If sUser = "" Then

Exit Function

End If

ReDim Preserve sNeirong(i)

sNeirong(i) = sUser

Next

End Function

Public Function FlLeftB(p1 As String, p2 As Long) As String

FlLeftB = StrConv(LeftB(StrConv(p1, vbFromUnicode), p2), vbUnicode)

End Function

给你两个读取ini文件的过程吧

'读取配置文件

Public

Declare

Function

GetPrivateProfileString

Lib

"kernel32"

Alias

"GetPrivateProfileStringA"

(ByVal

lpApplicationName

As

String,

ByVal

lpKeyName

As

Any,

ByVal

lpDefault

As

String,

ByVal

lpReturnedString

As

String,

ByVal

nSize

As

Long,

ByVal

lpFileName

As

String)

As

Long

'写入配置文件

Public

Declare

Function

WritePrivateProfileString

Lib

"kernel32"

Alias

"WritePrivateProfileStringA"

(ByVal

lpApplicationName

As

String,

ByVal

lpKeyName

As

Any,

ByVal

lpString

As

Any,

ByVal

lpFileName

As

String)

As

Long

Public

Function

ReadOption(ByVal

Caption

As

String,

ByVal

Item

As

String,

ByVal

Path

As

String)

As

String

'Caption小节名称;Item项名称;Path配置文件路径

On

Error

Resume

Next

Dim

sBuffer

As

String

sBuffer

=

Space(128)

GetPrivateProfileString

Caption,

Item,

vbNullString,

sBuffer,

128,

Path

ReadOption

=

Left(sBuffer,

InStr(sBuffer,

vbNullChar)

-

1)

End

Function

'写入配置文件

Public

Function

WriteOption(ByVal

Caption

As

String,

ByVal

Item

As

String,

ByVal

Key

As

String,

ByVal

Path

As

String)

'Caption小节名称;Item项名称;Key项值;Path配置文件路径

Dim

sBuffer

As

String

sBuffer

=

Space(128)

sBuffer

=

Key

&

vbNullChar

WriteOption

=

WritePrivateProfileString(Caption,

Item,

sBuffer,

Path)

End

Function

'获得配置文件路径

Public

Function

GetOptPath()

As

String

GetOptPath

=

App.Path

&

"\config.ini"

End

Function

Imports SystemImports 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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12026754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存