以下是操作 ini 文件的类代码,
如果写入
[text]
1=学业
就会乱码,再多存几个汉字,又能正常显示了.
我试了几个论坛上其他网友的程序,也存在这个问题,大家可以试试.
只存两个汉字:学业,
有的汉字也不出问题.
Option Explicit
'the path of our ini file
Private strINI As String
'ini文件的路径
'Windows API Declares
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
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 MakePath(ByVal strDrv As String, ByVal strDir As String) As String
' Makes an INI file: Guarantees a sub dir
Do While Right$(strDrv, 1) = "\"
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop
Do While Left$(strDir, 1) = "\"
strDir = Mid$(strDir, 2)
Loop
' Return the path
MakePath = strDrv & "\" & strDir
End Function
Private Sub CreateIni(strDrv As String, strDir As String)
' Make a new ini file
strINI = MakePath(strDrv, strDir)
End Sub
'--------------------------
'功能:写入ini文件
'创建:2007/8/7
'更新:2007/8/7 9:07
'--------------------------
Public Function WriteIniKey(ByVal strSection As String, ByVal strKey As String, ByVal strValue As String) As Boolean
On Error GoTo errhandle
' Write to strINI
WriteIniKey = CBool(WritePrivateProfileString(strSection, strKey, strValue, strINI))
Exit Function
errhandle:
MsgBox Err.Description
Err.Clear
End Function
'---------------------------
'功能:从ini文件中获取信息
'创建:2007/8/7
'更新:2007/8/7 8:55
'---------------------------
Public Function GetIniKey(strSection As String, strKey As String) As String
On Error GoTo errhandle
Dim strResult
As String * 255
'固定255长度
Dim lngRet
As Long
'先将strResult定义成1024个字符的长度,保证一定能装下返回的字串
'strResult = String$(1024, Chr(32))
'strResult = String$(1024, 0)
'strResult = Space(255)
'lpApplicationName String,欲在其中查找条目的小节名称。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载这个ini文件所有小节的列表
'lpKeyName String,欲获取的项名或条目名。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载指定小节所有项的列表
'lpDefault String,指定的条目没有找到时返回的默认值。可设为空("")
'lpReturnedString String,指定一个字串缓冲区,长度至少为nSize,返回的字符串
'nSize Long,指定装载到lpReturnedString缓冲区的最大字符数量,返回字符串的长度
'lpFileName String,初始化文件的名字。如没有指定一个完整路径名,windows就在Windows目录中查找文件
lngRet = GetPrivateProfileString(strSection, strKey, "", strResult, Len(strResult), strINI)
'strResult现在已经是返回的字串了,所以要进行截尾处理,chr(0)为空操作符,代表字符串的结束
GetIniKey = Left(strResult, InStr(strResult, Chr(0)) - 1)
Exit Function
errhandle:
GetIniKey = ""
End Function
Public Property Let INIFileName(ByVal New_IniPath As String)
' Sets the new ini path
strINI = New_IniPath
End Property
Public Property Get INIFileName() As String
' Returns the current ini path
INIFileName = strINI
End Property
'-----------------------------
'功能:清除KeyWord"键"
'创建:2007/8/7
'更新:2007/8/7 9:19
'-----------------------------
Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
'RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
RetVal = WritePrivateProfileString(SectionName, KeyWord, vbNullString, strINI)
End Function
'如果是清除section就少写一个Key多一个""。
'-----------------------------
'功能:清除 Section"段"
'创建:2007/8/7
'更新:2007/8/7 9:19
'-----------------------------
Public Function DelIniSec(ByVal SectionName As String) '清除section
Dim RetVal As Integer
'RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
'也可以实现
RetVal = WritePrivateProfileString(SectionName, vbNullString, vbNullString, strINI)
End Function