读取ini中Key值为中文的问题?

dgmichaelz 2006-02-24 11:53:26
vb.ini文件内容如下:
[test]
name=张三
age=23
idcard=440000000000

要求显示结果为:
msgbox输出
name:张三
age:23
idcard:44000000000
在VB中若按以上内容连续取值(不关闭VB.ini)显示
则只能显示name:张三
若将name的key放在最后取,则没有此问题,用GetPrivateProfileString取值时,size均设为256

这是什么原因,能否按顺序解决,遇到多个key值为中文的问题,我该如何处理?

...全文
803 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgmichaelz 2006-03-08
  • 打赏
  • 举报
回复
怎么给不了分,老是提示JAVASCRIPT错误
dgmichaelz 2006-02-25
  • 打赏
  • 举报
回复
jadeluo(秀峰) Ok
xiazh8282(信天翁) 没有详细试
Thank for everybody!
NewViewStudio 2006-02-25
  • 打赏
  • 举报
回复
其实这个问题主要的错误在于
这个GetPrivateProfileString API函数是用C写的,所以它应该遵循C的字符串结束的判断标志。
应该利用Left 和 Instr 函数查找返回字符串中的Chr(0)即可,其实GetPrivateProfileString的返回值在没有中文的时候是正确的,而有中文的时候是不正确的,因为Ansi和UniCode的问题造成这个错误。
jadeluo 2006-02-24
  • 打赏
  • 举报
回复
给你个做好的函数, 肯定能用的:

Option Explicit

Private 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

Private Function GetProString(段 As String, 标题 As String, 默认值 As String, 文件名 As String) As String
'功能描述: 读取配置文件内容
'入口参数:
' 段 - 所需读取的内容在配置文件中的段,如果是VbNullString,则返回值是所有段的名称
' 标题 - 所需读取的内容在该段中的标题,如果是VbNullString,则返回值是该段中所有的标题的名称
' 默认值 - 当未能找到段或者标题时,返回的默认值
' 文件名 - 配置文件的文件名
'出口参数:
' 返回值 - 如果能找到配置文件中该段的该标题内容,则返回这个内容;否则返回的是默认值
' 如果返回多个段或者标题的名称,则两个名称之间是由VbNullChar进行分隔的。
Const ProStringLen = 8192
Dim Res As Long, S As String, i As Long
S = Space(ProStringLen)
Res = GetPrivateProfileString(段, 标题, 默认值, S, ProStringLen, 文件名)
S = Trim(Left(S, Res))
'去除含有汉字的字符串末尾的0号字符
If S <> "" Then
i = Len(S)
Do While Mid(S, i, 1) = vbNullChar
i = i - 1
Loop
S = Left(S, i)
End If
GetProString = Trim(S)
End Function

Private Sub Form_Load()
Debug.Print GetProString("Test", "name", "", "F:\VB.INI")
Debug.Print GetProString("Test", "age", "", "F:\VB.INI")
Debug.Print GetProString("Test", "idcard", "", "F:\VB.INI")
End
End Sub
xiazh8282 2006-02-24
  • 打赏
  • 举报
回复
给你一个模块

Option Explicit


'************************************************
'**
'** INI 文件读、写类
'**
'************************************************

'// 读取INI文件
Private 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
'// 写入INI文件
Private 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

'************************************************
'** 函数名称: WriteINI
'** 函数功能: 将信息写入Ini文件
'** 参数说明:
'** strHeadName INI文件的表头,就是[]里的内容
'** strListName “=”号左边的名称
'** strFileName INI文件名
'** strValue “=”号右边的值
'** strExplain 说明,可选的
'** 函数返回:
'** 无返回值
'** 参考实例:
'**
'************************************************
Public Sub WriteINI(ByVal strHeadName As String, _
ByVal strListName As String, _
ByVal strFileName As String, _
ByVal strValue As String, _
Optional ByVal strExplain As String = "")

Dim lngFileNum As Long

'// 判断文件是否已经存在
If FileOrFolderExists(strFileName, vbNormal) = False Then
'// 如果文件不存在,则创建一个新的文件

lngFileNum = FreeFile
Open strFileName For Output As #lngFileNum
Close #lngFileNum
End If

strValue = Replace(strValue, vbCrLf, "<S:BR>")

Call WritePrivateProfileString(strHeadName, strListName, _
strValue & ";" & strExplain, strFileName)
End Sub

'************************************************
'** 函数名称: GetINI
'** 函数功能: 读文件过程
'** 参数说明:
'** strHeadName INI文件的表头,就是[]里的内容
'** strListName “=”号左边的名称
'** strFileName INI文件名
'** strDefault 当没有值时返回的默认值
'** intValueLen 值的长度(可选)
'** 函数返回:
'** String 类型
'** strDefault 当没有值时返回用户指定的默认值
'** 实际内容 正确取得的值
'** 参考实例:
'**
'************************************************
Public Function GetINI(ByVal strHeadName As String, _
ByVal strListName As String, _
ByVal strFileName As String, _
Optional ByVal strDefault As String = "", _
Optional ByVal intValueLen As Integer = 256) As String
Dim strValue As String
Dim strTemp As String
Dim lngReturn As Long

'// 设置缓冲
strValue = String(intValueLen, Space(1))
'// 取值
lngReturn = GetPrivateProfileString(strHeadName, strListName, "", _
strValue, Len(strValue), strFileName)
strTemp = ""

If InStr(strValue, ";") > 0 Then
strTemp = Left$(strValue, InStr(strValue, ";") - 1)
ElseIf lngReturn > 0 Then
strTemp = Left$(strValue, lngReturn)
End If

If strTemp = "" Then
GetINI = strDefault
Else
GetINI = Replace(strTemp, "<S:BR>", vbCrLf)
End If
End Function

'************************************************
'** 函数名称: FileOrFolderExists
'** 函数功能: 判断文件或目录是否存在
'** 参数说明:
'** strName 文件或目录名称
'** intFileAttribute 文件或目录类型(隐藏、只读等)
'** 函数返回:
'** Boolean 类型
'** True 文件或目录存在
'** False 文件或目录不存在
'** 参考实例:
'**
'************************************************
Public Function FileOrFolderExists(ByVal strName As String, _
ByVal intFileAttribute As VbFileAttribute) As Boolean
On Error Resume Next

'// 如果指定的文件或目录名称为空则退出函数
If Len(strName) = 0 Then
FileOrFolderExists = False
Exit Function
End If

'// 如果为空表示文件或目录不存在
If Dir(strName, intFileAttribute) = "" Then
FileOrFolderExists = False
Else
FileOrFolderExists = True
End If
End Function


1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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