首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

请问一个读取ini文件的有关问题,多谢

2012-01-13 
请教一个读取ini文件的问题,谢谢!Pwd@# B$D !$-#!%%%$&H--.F/ ,M+80 .J.P5O.K5比如我的ini文

请教一个读取ini文件的问题,谢谢!
Pwd=@# "B$D "!$-#   ";!%%;%$&H--.F/> ,M+80> .J.P5O.K5

比如我的ini文件保存有上面的信息,但是我的读取ini文件的类读出来Pwd的值怎么没有前面的,如果我吧放到中间也能读出来,但是当在末尾或者前面就读不出来,这是为什么呢?我读ini文件的类如下:


Option   Explicit

Private   Declare   Function   GetPrivateProfileInt   Lib   "kernel32 "   Alias   "GetPrivateProfileIntA "   (   _
ByVal   lpApplicationName   As   String,   _
ByVal   lpKeyName   As   String,   _
ByVal   nDefault   As   Long,   _
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   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   strFileName   As   String
Public   strIniErr

'初始化类
Private   Sub   Class_Initialize()
        strFileName   =   vbNullString
End   Sub

'判断是否指定INI文件
Private   Function   NoIniFile()   As   Boolean
        NoIniFile   =   True
        If   strFileName   =   vbNullString   Then
                strIniErr   =   "尚未指定INI文件! "
                Exit   Function
        End   If
        strIniErr   =   vbNullString
        NoIniFile   =   False
End   Function

'获取INI文件
Public   Sub   LetFile(strFile)
        strFileName   =   Trim(strFile)
End   Sub


'开始写
Public   Function   WriteString(Section   As   String,   key   As   String,   Value   As   String)   As   Boolean
        WriteString   =   False
        If   NoIniFile()   Then
                Exit   Function
        End   If
        If   WritePrivateProfileString(Section,   key,   Value,   strFileName)   =   0   Then
                strIniErr   =   "写入INI文件( "   &   strFileName   &   ")失败! "
                Exit   Function
        End   If
        WriteString   =   True


End   Function

Public   Function   ReadString(Section   As   String,   key   As   String,   Size   As   Long)   As   String
        Dim   ReturnStr   As   String
        Dim   ReturnLng   As   Long
        ReadString   =   vbNullString
        If   NoIniFile()   Then
                Exit   Function
        End   If
        ReturnStr   =   Space(Size)
        ReturnLng   =   GetPrivateProfileString(Section,   key,   vbNullString,   ReturnStr,   Size,   strFileName)
        ReadString   =   Replace(Trim(Left(ReturnStr,   ReturnLng)),   Chr(0),   " ")
End   Function

Public   Function   ReadInt(Section   As   String,   key   As   String)   As   Long
        Dim   ReturnLng   As   Long
        ReadInt   =   0
        ReturnLng   =   GetPrivateProfileInt(Section,   key,   0,   strFileName)
        If   ReturnLng   =   0   Then
                ReturnLng   =   GetPrivateProfileInt(Section,   key,   1,   strFileName)
                If   ReturnLng   =   1   Then
                        strIniErr   =   "不能读取INI文件( "   &   strFileName   &   ") "
                        Exit   Function
                End   If
        End   If
        ReadInt   =   ReturnLng
End   Function




[解决办法]
這樣的東東一般都是copy
看來也有必要研究一下了

[解决办法]
Function Read_INI(szAP_Name As String, szKey_Name As String) As String
Dim szWork As String
Dim lRet As Long, szRet_Str As String, lRer_Size As Long, szFI_Name
'读取
szFI_Name = P_szIniPath + "Collect.ini "
szRet_Str = String(255, " ")
lRet = GetPrivateProfileString(szAP_Name, szKey_Name, szWork, szRet_Str, 255, szFI_Name)
If lRet < 0 Then
Read_INI = " "
Else
Read_INI = Mid(szRet_Str, 1, lRet)
End If

End Function


Public Function Write_INI(szIniDir As String, szTitle As String, szKey As String, szFlag As String) As Integer

Dim lRet As Long
Dim szFI_Name As String

On Error GoTo write_ini_err

Write_INI = -1

'INI写入
szFI_Name = P_szIniPath + "Collect.ini "
lRet = WritePrivateProfileString(szIniDir, szTitle, szKey, szFI_Name)

If lRet <> 0 Then
Exit Function
End If
Write_INI = 0

Exit Function

write_ini_err:

End Function



[解决办法]
INI文件中是这样的吗?
[aaa]
Pwd=@# "B$D "!$-# ";!%%;%$&H--.F/> ,M+80> .J.P5O.K5

我可以读得出啊
代码如下:

Private Declare Function getIniStr Lib "inis.dll " (ByVal Files As String, ByVal Section As String, ByVal Ident As String) As String

Private Sub Command1_Click()
MsgBox getIniStr(App.Path & "\sys.ini ", "aaa ", "pwd ")
End Sub

我用的不是VBAPI,而是自己开发的DLL。
API的从来不用的,所以也不知道怎么解决API的。呵呵


[解决办法]
'一个类,名称为ClsIniFile
Option Explicit


'Private member that holds a reference to
'the path of our ini file

Private strINI As String

'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

Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
' Write to strINI
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub

Public Function GetIniKey(strSection As String, strKey As String) As String
Dim strTmp As String
Dim lngRet As String
Dim i As Integer
Dim strTmp2 As String

strTmp = String$(1024, Chr(32))
lngRet = GetPrivateProfileString(strSection, strKey, " ", strTmp, Len(strTmp), strINI)
strTmp = Trim(strTmp)
strTmp2 = " "
For i = 1 To Len(strTmp)
If Asc(Mid(strTmp, i, 1)) <> 0 Then
strTmp2 = strTmp2 + Mid(strTmp, i, 1)
End If
Next i
strTmp = strTmp2

GetIniKey = strTmp
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 "键 "(Sub)***********************************************
Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Function



'如果是清除section就少写一个Key多一个 " "。
'**************************************清除 Section "段 "(Sub)***********************************************
Public Function DelIniSec(ByVal SectionName As String) '清除section
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, 0&, " ", strINI)
End Function


Form中
dim IniFile as new ClsIniFile
IniFile.INIFileName = App.Path & "\SysSet.ini "
IniFile.WriteIniKey "Test ", "Pwd ", "@# "B$D "!$-# ";!%%;%$&H--.F/> ,M+80> .J.P5O.K5 "
Debug.Print IniFile.GetIniKey( "Test ", "PWD ")
set Inifile =nothing

热点排行