API函数读取INI文件

001chb 2002-02-26 01:45:48
看很多资料,都说GetPrivateProfileSection可以读取INI指定小节所有项名和值的一个列表。我在模块中声明该函数后,发现运行下面语句
ss=space(50)
GetPrivateProfileSection "income",ss,50,"data.ini"
函数的返回值为所有小节的总字符数,len(ss)=50;
但len(trim(ss))=0
为什么?这个函数到底该怎么用,希望能提供示例。
001chb@sina.com
请教各位,先谢了!
...全文
159 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
001chb 2002-02-27
  • 打赏
  • 举报
回复
谢谢各位,问题已经解决!
我先是在立即窗口中运行,就有我所说的问题,在程序中测试就很正常。很奇怪!
另外有一个也很奇怪的问题:
我是想在INI文件中保存用户对系统参数的一些设置,开始改动时,我把 filecopy "data.ini" ,'tmp.ini" ;当用户改动暂时记录在tmp.ini中,确定要保存时,先kill "data.ini" ;接着 filecopy 'tmp.ini" , "data.ini"
可是在程序中 kill "data.ini" 和 filecopy 'tmp.ini" , "data.ini" 似乎没有起作用,如果连续单击两次确定按钮就可以保存。部分源代码如下,其它部分不涉及对ini文件的改动

Dim typename As String '改动的小节名称

Private Sub cmdclose_Click()
'窗口关闭
Unload Me
End Sub

Private Sub cmdedit_Click()
'把用户改动暂时记录在tmp.ini中
List1.List(List1.ListIndex) = txtdata
WritePrivateProfileString typename, CStr(List1.ListIndex + 1), txtdata, App.Path & "\tmp1.ini"
cmdedit.Enabled = False
issave = False
End Sub

Private Sub cmdsave_Click()
'单击确定按钮,确定保存
Kill App.Path & "\data.ini"
FileCopy App.Path & "\tmp1.ini", App.Path & "\data.ini"
issave = True
End Sub

Private Sub Form_Load()
FileCopy App.Path & "\data.ini", App.Path & "\tmp1.ini"
…………
…………
End Sub

Private Sub Form_Unload(Cancel As Integer)
Kill App.Path & "\tmp1.ini"
End Sub
………………
………………

001chb 2002-02-26
  • 打赏
  • 举报
回复
其他的读写ini文件的api函数都正常
001chb 2002-02-26
  • 打赏
  • 举报
回复
to 111222(www.mozilla.org)
我就是按你所说的那样写的,但是还是不行!在debug窗口中
print GetPrivateProfileSection("ide", szBuf, 255, App.Path & "\bcw5.ini")
50 '返回值说明已经读取到数据
print szBuf
'这里就是50个空格!


uguess 2002-02-26
  • 打赏
  • 举报
回复
'Visit his site at http://members.fortunecity.com/rbnwares1
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Sub Form_Load()
' We will create a new section in the INI-file
' It's output will be:
'
' [SectionName]
' Key=Value
'
' Note that used this ONLY if you are creating a new
' section on the INI file, unless you wanted to erase
' its existing keys.
Call WritePrivateProfileSection("SectionName", "Key=Value", App.Path & "\sample.ini")
Dim szBuf As String * 255
Call GetPrivateProfileSection("SectionName", szBuf, 255, App.Path & "\sample.ini")
MsgBox szBuf
End Sub
111222 2002-02-26
  • 打赏
  • 举报
回复

只需要人民币20元,

你就可以获得本灌水机的程序和Visual C++源代码,

并获得永久性技术支持。

还等什么?灌水等着你!

就在今夜!元宵佳节!

(可怜我信誉)
001chb 2002-02-26
  • 打赏
  • 举报
回复
to shawls(小山(坚持VB,学Delphi和C#))
从函数返回值看,应该取得了数据,但为什么全部是""?
zyl910 2002-02-26
  • 打赏
  • 举报
回复
Option Explicit
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
Public 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


'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'WriteValue:值
Public Function WriteIni(FileName As String, _
PathName As String, _
KeyName As String, _
WriteValue As String) As Long
Dim Rc As Long

Rc = WritePrivateProfileString(PathName, KeyName, WriteValue, FileName)

WriteIni = Rc

End Function

'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'BackValue:返回值
'Default:默认字符
Public Function ReadIni(FileName As String, _
PathName As String, _
KeyName As String, _
BackValue As String, _
Optional Default As String = "缺省") As Long
Dim Rc As Long
Dim TempNum As String
Dim TempStr As String

TempStr = String$(255, Chr$(0))
TempNum = 255

Rc = GetPrivateProfileString(PathName, KeyName, Default, TempStr, TempNum, FileName)

If Rc <> 0 Then
BackValue = Left$(TempStr, TempNum)

End If

ReadIni = Rc

End Function
MarGo 2002-02-26
  • 打赏
  • 举报
回复
dim re as long
dim restr as string
getstr=space(256)
re=GetPrivateProfileString ("Setting", In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini")
restr=left(1,len(trim(getstr))-1))
以上是经过运行的代码,试试吧

shawls 2002-02-26
  • 打赏
  • 举报
回复


很简单:没有取得数据!!

你仔细分析一下我的那个函数!!!
001chb 2002-02-26
  • 打赏
  • 举报
回复
在线等待!
一小时4.2元,我是拨号!
001chb 2002-02-26
  • 打赏
  • 举报
回复
我的问题是为什么SS在读取INI文件后还是len(trim(ss))=0?
为什么不是我读取的数据列表啊!


001chb 2002-02-26
  • 打赏
  • 举报
回复
我的问题是为什么SS在读取INI文件后还是len(trim(ss))?
为什么不是我读取的数据列表啊!
pengnick 2002-02-26
  • 打赏
  • 举报
回复
没错
shawls 2002-02-26
  • 打赏
  • 举报
回复

trim作用:删除字符串前后空格!!
shawls 2002-02-26
  • 打赏
  • 举报
回复
读写INI文件的四个函数

'文件名SourceDB.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
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

'以下两个函数,读/写ini文件,固定节点setting,in_key为写入/读取的主键
'仅仅针对是非值
'Y:yes,N:no,E:error
Public Function GetIniTF(ByVal In_Key As String) As Boolean
On Error GoTo GetIniTFErr
GetIniTF = True
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString "Setting", In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "1" Then
GetIniTF = True
GetStr = ""
Else
GoTo GetIniTFErr
End If
Exit Function
GetIniTFErr:
Err.Clear
GetIniTF = False
GetStr = ""
End Function

Public Function WriteIniTF(ByVal In_Key As String, ByVal In_Data As Boolean) As Boolean
On Error GoTo WriteIniTFErr
WriteIniTF = True
If In_Data = True Then
WritePrivateProfileString "Setting", In_Key, "1", App.Path & "\SourceDB.ini"
Else
WritePrivateProfileString "Setting", In_Key, "0", App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniTFErr:
Err.Clear
WriteIniTF = False
End Function


'以下两个函数,读/写ini文件,不固定节点,in_key为写入/读取的主键
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function

Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function


以上代码来自: 源代码数据库(SourceDataBase)
当前版本: 1.0.448
作者: Shawls
个人主页: Http://Shawls.Yeah.Net
E-Mail: ShawFile@163.Net
QQ: 9181729

1,488

社区成员

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

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