读写INI文件时的错误,实在不好解决,请哪位大虾指教??

flxa 2002-09-11 03:18:13
我用GetPrivateProfileString读INI文件的有关内容是,总是读不出来,我是在VB里面用的,INI的Section Name为@System,Key为UDL,可是读出来的时候总是为空不知道为什么!其他参数也都已经设置完成!
...全文
38 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
阿甘 2002-09-11
VB6里面就自带有注册表操作模块,为什么不用呢?
回复
smilejiangjun 2002-09-11
Option Explicit

Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Option Explicit

Private Sub Command1_Click()'读取[windows] programs字符串
Dim S As String, Length As Long

S = String(1024, 0)
Length = GetProfileString("windows", "programs", "", S, Len(S))
S = Left(S, Length)

List1.Clear
List1.AddItem "programs=" & S
End Sub

Private Sub Command2_Click()'读取[windows]的所有Key
Dim S As String, Length As Long, pos As Integer

S = String(1024, 0)
Length = GetProfileString("windows", vbNullString, "", S, Len(S))
S = Left(S, Length)

List1.Clear
While Len(S) > 0
pos = InStr(S, Chr(0))
List1.AddItem Left(S, pos - 1)
S = Mid(S, pos + 1)
Wend
End Sub

Private Sub Command3_Click()'读取Win.ini的所有Section
Dim S As String, Length As Long, pos As Integer

S = String(1024, 0)
Length = GetProfileString(vbNullString, vbNullString, "", S, Len(S))
S = Left(S, Length)

List1.Clear
While Len(S) > 0
pos = InStr(S, Chr(0))
List1.AddItem Left(S, pos - 1)
S = Mid(S, pos + 1)
Wend
End Sub
回复
flxa 2002-09-11
可是就是不行!和操作系统有关系吗?!

回复
asdfsdfrewrwesr 2002-09-11
我有一个读写INI文件的模块,如果需要,你可以发邮件到rsidemail@163.com索取。 :)
回复
zqfleaf 2002-09-11

Option Explicit
'INI文件的文件名
Public File As String

#If Win32 Then
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 Integer, ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal FileName As String) As Integer
#Else
Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal FileName As String) As Integer
#End If

'删除一个[OPITION]
Sub DeleteSection(ByVal Section As String)

Dim retval As Integer

retval = WritePrivateProfileString(Section, 0&, "", File)

End Sub
'保存一个key
Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)

Dim retval As Integer

SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)

End Function
'得到一个key值
Public Function GetSettingB(ByVal Section As String, ByVal KeyName As String) As String

Dim retval As Integer
Dim t As String * 255
retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File)
If retval > 0 Then
GetSettingB = Left$(t, retval)
Else
GetSettingB = "Unknown section or key"
End If
End Function

'得到一个[OPITION]
Public Function GetSection(ByVal Section As String, KeyArray() As String) As Integer

Dim retval As Integer
Dim t As String * 2500
Dim lastpointer As Integer
Dim nullpointer As Integer
Dim ArrayCount As Integer
Dim keystring As String

ReDim KeyArray(0)

retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File)

' If there is one, return it
If retval > 0 Then
'
' Separate the keys and store them in the array
nullpointer = InStr(t, Chr$(0))
lastpointer = 1
Do While (nullpointer <> 0 And nullpointer > lastpointer + 1)
'
' Extract key string
keystring = Mid$(t, lastpointer, nullpointer - lastpointer)
'
' Now add to array
ArrayCount = ArrayCount + 1
ReDim Preserve KeyArray(ArrayCount)
KeyArray(ArrayCount) = keystring
'
' Find next null
lastpointer = nullpointer + 1
nullpointer = InStr(nullpointer + 1, t, Chr$(0))
Loop
End If
'
' Return the number of array elements
GetSection = ArrayCount

End Function



回复
flxa 2002-09-11
运行环境是WIN98+VB6
回复
nik_Amis 2002-09-11
Public Function PutToINI(ByVal sApp As String, ByVal sKey As String, ByVal sValue As String) As Boolean
Dim lReturn As Long
Dim sFilename As String

sFilename = App.Path & "\" & gcsINIFileName
lReturn = WritePrivateProfileString(sApp, sKey, sValue, sFilename)
PutToINI = (lReturn = 1)
End Function

Public Function GetInINI(ByVal sApp As String, ByVal sKey As String, ByRef sValue As String) As Boolean
Dim lReturn As Long
Dim sBuffer As String
Dim sFilename As String

On Error Resume Next
sFilename = App.Path & "\" & gcsINIFileName
sBuffer = String(1024, 0)
lReturn = GetPrivateProfileString(sApp, sKey, "", sBuffer, 1024, sFilename)
sValue = Left(sBuffer, lReturn)
GetInINI = (Err.Number = 0)
End Function
回复
发帖
VB基础类
创建于2007-09-28

7476

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2002-09-11 03:18
社区公告
暂无公告