如何在vb中编写读取ini文件的代码???
同上。。。 问题点数:40、回复次数:4Top
1 楼gump2000(阿甘)回复于 2002-04-15 10:46:31 得分 20
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 AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal Filename As String) As Long
Private sDefInitFileName As String
'读取Ini文件
Public Function GetInitEntry(ByVal sSection As String, ByVal sKeyName As String, Optional ByVal sDefault As String = "", Optional ByVal sInitFileName As String = "") As String
Dim sBuffer As String
Dim sInitFile As String
If Len(sInitFileName) = 0 Then
If Len(sDefInitFileName) = 0 Then
sDefInitFileName = App.Path
If Right$(sDefInitFileName, 1) <> "\" Then
sDefInitFileName = sDefInitFileName & "\"
End If
sDefInitFileName = sDefInitFileName & App.EXEName & ".ini"
End If
sInitFile = sDefInitFileName
Else
sInitFile = sInitFileName
End If
sBuffer = String$(2048, " ")
GetInitEntry = Left$(sBuffer, GetPrivateProfileString(sSection, ByVal sKeyName, sDefault, sBuffer, Len(sBuffer), sInitFile))
End Function
'写Ini文件
Public Function SetInitEntry(ByVal sSection As String, Optional ByVal sKeyName As String, Optional ByVal sValue As String, Optional ByVal sInitFileName As String = "") As Long
Dim sInitFile As String
If Len(sInitFileName) = 0 Then
If Len(sDefInitFileName) = 0 Then
sDefInitFileName = App.Path
If Right$(sDefInitFileName, 1) <> "\" Then
sDefInitFileName = sDefInitFileName & "\"
End If
sDefInitFileName = sDefInitFileName & App.EXEName & ".ini"
End If
sInitFile = sDefInitFileName
Else
sInitFile = sInitFileName
End If
If Len(sKeyName) > 0 And Len(sValue) > 0 Then
SetInitEntry = WritePrivateProfileString(sSection, ByVal sKeyName, ByVal sValue, sInitFile)
ElseIf Len(sKeyName) > 0 Then
SetInitEntry = WritePrivateProfileString(sSection, ByVal sKeyName, vbNullString, sInitFile)
Else
SetInitEntry = WritePrivateProfileString(sSection, vbNullString, vbNullString, sInitFile)
End If
End Function
Top
2 楼rivershan(阿门)回复于 2002-04-15 10:56:39 得分 10
需要模块~
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPriviteProfileIntA" (ByVal lpApplicationname As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Global file
Global appname
Global Keyname
Global value
Public Sub writeini()
Dim lpAppName As String, lpFileName As String, lpKeyName As String, lpString As String
Dim U As Long
lpAppName = appname
lpKeyName = Keyname
lpString = value
lpFileName = file
U = WritePrivateProfileString(lpAppName, lpKeyName, lpString, lpFileName)
If U = 0 Then
Beep
End If
End Sub
Public Sub readini()
Dim X As Long
Dim Temp As String * 50
Dim lpAppName As String, lpKeyName As String, lpDefault As String, lpFileName As String
lpAppName = appname
lpKeyName = Keyname
lpDefault = no
lpFileName = file
X = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, Temp, Len(Temp), lpFileName)
If X = 0 Then
Beep
Else
result = Trim(Temp)
End If
End Sub
Top
3 楼shaoqiang(小邵)回复于 2002-04-15 11:01:59 得分 10
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, 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 ReadINI(strsection As String, strkey As String, strfullpath As String) As String
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'read ini '
'x = readini("Example INI", "Example", "C:\Example.ini")'
'MsgBox X '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Public Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'write ini '
'Call writeini("Example INI", "Example", "Yes", "C:\Example.ini")'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Top
4 楼Chimae(David)回复于 2002-04-15 11:22:54 得分 0
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
Dim lngIni As Long
lngIni = GetPrivateProfileString(strAppName, strKeyName, "", strReturnValue, Len(strReturnValue), strIniPath)
If lngIni <> 0 Then
If Len(strReturnValue) <> 0 Then
strTemp = Left(strReturnValue, lngIni)
End If
End If
strReturnValue是用来返回值得,最好初始化时长一点;这是我自己的是用体会!Top




