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

flxa 2002-09-11 03:18:13
我用GetPrivateProfileString读INI文件的有关内容是,总是读不出来,我是在VB里面用的,INI的Section Name为@System,Key为UDL,可是读出来的时候总是为空不知道为什么!其他参数也都已经设置完成!
...全文
74 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿甘 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
纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。 现将本人精心制作的ini读写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。 主要特点: 1、支持;和#注释符号,支持行尾注释。 2、支持带引号'或"成对匹配的字符串,提取自动去引号。引号中可带其它引号或;#注释符。 3、支持无section或空section(名称为空)。 4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。 5、支持section、key或=号前后带空格。 6、支持\n、\r、\r\n或\n\r换行格式。 7、不区分section、key大小写,但写入以新串为准,并保持其大小写。 8、新增数据,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。 9、支持指定key所在整行删除,即删除该键值,包括注释。 10、可自动跳过格式错误行,修改仍然保留。 11、修改保留原注释:包括整行注释、行尾注释(包括前面空格)。 12、修改保留原空行。以上三点主要是尽量保留原格式。 不足之处: 1、不支持单key多value(逗号分割),只能一次性提取后自行处理。 2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧) 3、不能提取所有section或key名称。 使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux。

7,785

社区成员

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

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