求读写ini文件的例子

多难成佛 2008-12-22 02:22:16
设启动窗体Form1有ComboBox1,CheckBox1两个控件,INI.ini,利用INI.ini文件实现以下功能:
如果CheckBox1.Value=1,将ComboBox1.Text写入INI.ini/Section1/Key1,下次启动时先读取INI.ini/Section1/Key1,
如果键值存在,则ComboBox1.Text=INI.ini/Section1/Key1,否则ComboBox1.Text=“默认配置”。
我在网上找到一篇帖子,不是很明白,请高手指教!

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
' 返回所读取的长整型值 Alias "GetPrivateProfileIntA"
' 要读取的段 (Section) 名称 ByVal lpApplicationName As String
' 要读取的的键 (Key) 名称 ByVal lpKeyName As String
' 指定默认值,如果读取时出错,则返回该值 ByVal nDefault As Long
' 指定要读的 INI 文件名 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
' 返回所读取的字符串值的真实长度 Alias "GetPrivateProfileStringA"
' 要读取的段 (Section) 名称 ByVal lpApplicationName As String
' 要读取的的键 (Key) 名称 ByVal lpKeyName As Any
' 指定默认值,如果读取时出错,则返回该值 ByVal lpDefault As String
' 指定接收返回值的字符串变量 ByVal lpReturnedString As String
' 指定允许字符串值的最大长度 ByVal nSize As Long
' 指定要读的 INI 文件名 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
' 如果成功返回非 0 值,失败返回 0 Alias "WritePrivateProfileStringA"
' 要写入的段 (Section) 名称 ByVal lpApplicationName As String
' 要写入的的键 (Key) 名称 ByVal lpKeyName As Any
' 要写入的值 (Value),以字符串表示 ByVal lpString As Any
' 指定要写的 INI 文件名 ByVal lpFileName As String) As Long
'============================================================================
Private IniFileName As String
Public ErrorMsg As String
Private Sub Class_Initialize()
IniFileName = vbNullString
ErrorMsg = vbNullString
End Sub
Public Sub SpecifyIni(FilePathName)
IniFileName = Trim(FilePathName)
End Sub
'判断是否指定了ini文件
Private Function NoIniFile() As Boolean
NoIniFile = True
If IniFileName = vbNullString Then
ErrorMsg = "没有指定 INI 文件"
Exit Function
End If
ErrorMsg = vbNullString
NoIniFile = False
End Function
'写入
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, IniFileName) = 0 Then
ErrorMsg = "写入失败"
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, IniFileName)
ReadString = Left(ReturnStr, ReturnLng)
End Function
Public Function ReadInt(Section As String, key As String) As Long
Dim ReturnLng As Long
ReadInt = 0
ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName)
If ReturnLng = 1 Then
ErrorMsg = "不能读取"
Exit Function
End If
End If
ReadInt = ReturnLng
End Function
...全文
140 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
多难成佛 2008-12-23
  • 打赏
  • 举报
回复
明白一些了,谢谢各位
还有个小问题请教:
如何判断某个key的值(KeyVal)是否为空呢?
我用ReadKeyVal函数读取KeyVal怎么不对呢?
Function ReadKeyVal(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As String
Dim RetVal As String, GetKeyVal As String, Worked As Integer
GetKeyVal = ""
RetVal = String$(255, Chr(0))
Worked = GetPrivateProfileString(Section, Key, "", RetVal, Len(RetVal), FileName)
ReadKeyVal = IIf(Worked = 0, "", Left(RetVal, InStr(RetVal, Chr(0)) - 1))
End Function


KyVl = ReadKeyVal(IniFile, Sect, Key)'这里提示类型不匹配
If Not KyVl = True Then Form1.Check1.Visible = True
hzlwj 2008-12-23
  • 打赏
  • 举报
回复

Public Const MAX_FILENAME_LEN = 256

Dim buff As String * SIZE_PROFILE_DATA
Dim len_buff As Long

'读.INI文件
Public Declare Function GetProfileStr Lib "kernel32" _
Alias "GetPrivateProfileStringA" (ByVal SecName As String, _
ByVal EntrName As String, _
ByVal DefString As String, _
ByVal buff As String, _
ByVal buffsize As Long, _
ByVal filename As String) As Long
'写.INI文件
Public Declare Function SetProfileStr Lib "kernel32" _
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long


len_buff = GetProfileStr("SoftInfo", "SysName", "", buff, SIZE_PROFILE_DATA, TemFileName)
多难成佛 2008-12-23
  • 打赏
  • 举报
回复
模块代码:

Option Explicit

'声明读取ini的API
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
'声明写入ini的API
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
'检测ini文件是否存在的类
Const OFS_MAXPATHNAME = 128 '路径最大字符串二进制值
Const OF_EXIST = &H4000
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type

Private typOfStruct As OFSTRUCT
'声明打开文件对象的API
Declare Function apiOpenFile Lib "kernel32" Alias "OpenFile" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Public IniFile As String
Dim Sect As Long
Dim Key As Long
Public KyVl As String

Sub Main()
Dim MkIni As Long
IniFile = App.Path & "\aa.ini"
If Not FileExists(IniFile) = True Then '如果文件不存在
MsgBox "no files"
MkIni = WritePrivateProfileString("Branch", "Brannm", "", IniFile) '创建新文件
Form1.Check1.Visible = True
End If

Sect = SectionExists(IniFile, "Branch") '检测项目是否存在
Key = KeyExists(IniFile, "Branch", "") '检测键名是否存在
KyVl = ReadKeyVal(IniFile, "Branch", "Brannm") '读取键值到变量KyVl

If Not Sect = True Then '如果项目不存在
MsgBox "不存在", , "Sect"
Kill IniFile '删除原文件
MkIni = WritePrivateProfileString("Branch", "Brannm", "", IniFile) '创建新文件
Form1.Check1.Visible = True
Else
MsgBox Sect, , "Sect"
Form1.Check1.Visible = False
End If

If Not Key = True Then '如果键名不存在
MsgBox "不存在", , "Key"
Kill IniFile '删除原文件
MkIni = WritePrivateProfileString("Branch", "Brannm", "", IniFile) '创建新文件
Form1.Check1.Visible = True
Else
MsgBox Key, , "Key"
Form1.Check1.Visible = False
End If


If KyVl = "" Then '如果键值为空
MsgBox "不存在", , "KyVl"
Form1.Check1.Visible = True '显示Check1
Else
MsgBox KyVl
Form1.Check1.Visible = False
End If
Form1.Show
End Sub

'检测ini文件是否存在的函数
Public Function FileExists(ByVal sFilename As String) As Boolean
If Len(sFilename) > 0 Then
apiOpenFile sFilename, typOfStruct, OF_EXIST
FileExists = typOfStruct.nErrCode <> 2
End If
End Function

'检测TextLine是否是Key行
Private Function IsKey(ByVal TextLine As String) As Boolean
Dim Looper As Integer
IsKey = False
If TextLine = "" Then Exit Function
For Looper = 1 To Len(TextLine)
If Mid(TextLine, Looper, 1) = "=" Then IsKey = True: Exit For
Next Looper
If InStr(TextLine, "=") <> 0 Then IsKey = True
End Function

'检测TextLine是否是Section行
Private Function IsSection(ByVal TextLine As String) As Boolean
Dim FirstChar As String, LastChar As String
IsSection = False
If TextLine = "" Then Exit Function
FirstChar = Mid(TextLine, 1, 1)
LastChar = Mid(TextLine, Len(TextLine), 1)
If FirstChar = "[" And LastChar = "]" Then IsSection = True
End Function

'检测Key是否存在的函数
Public Function KeyExists(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As Boolean
Dim InZone As Boolean
Dim InputData As String
Dim Looper As Integer
Dim fNum As Byte
KeyExists = False
fNum = FreeFile
InZone = False
Open FileName For Input As #fNum
Do While Not EOF(fNum)
Line Input #fNum, InputData
If InZone Then
If IsKey(InputData) Then
If Left(InputData, Len(Key)) = Key Then
KeyExists = True
Exit Do
End If
ElseIf IsSection(InputData) Then
Exit Do
End If
Else
If InputData = "[" & Section & "]" Then InZone = True
End If
Loop
Close #fNum
End Function

'检测Section是否存在的函数
Public Function SectionExists(ByVal FileName As String, ByVal Section As String)
Dim InputData As String
Dim fNum As Byte
Dim MkIni As Integer
SectionExists = False
fNum = FreeFile
Open FileName For Input As #fNum
Do While Not EOF(fNum)
Line Input #fNum, InputData
If "[" & Section & "]" = InputData Then SectionExists = True: Exit Do
Loop
Close #fNum
End Function

'写入KeyVal的函数
Function WriteKeyVal(ByVal FileName As String, ByVal Section As String, ByVal Key As String, ByVal KeyValue As String) As Long
WriteKeyVal = 0
WriteKeyVal = WritePrivateProfileString(Section, Key, KeyValue, FileName)
End Function

'读取KeyVal的函数
Function ReadKeyVal(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As String
Dim RetVal As String, GetKeyVal As String, Worked As Integer
GetKeyVal = ""
RetVal = String$(255, Chr(0))
Worked = GetPrivateProfileString(Section, Key, "", RetVal, Len(RetVal), FileName)
ReadKeyVal = IIf(Worked = 0, "", Left(RetVal, InStr(RetVal, Chr(0)) - 1))
End Function


窗体代码:

Option Explicit

Private Sub Form_load()
If KyVl <> "" Then
Combo1.Text = KyVl
Else
Combo1.Text = "贵阳"
End If
End Sub

Private Sub Command1_Click()
If Check1.Value = 1 Then
Dim WriteKyVl As String
WriteKyVl = WriteKeyVal(IniFile, "Branch", "Brannm", Combo1.Text)
End If
End
End Sub

总算搞定了
oyljerry 2008-12-22
  • 打赏
  • 举报
回复
WritePrivateProfileString --- 写ini文件的字符串

GetPrivateProfileString --- 读ini文件的字符串

GetPrivateProfileInt --- 读ini文件的数字
xrongzhen 2008-12-22
  • 打赏
  • 举报
回复
楼主找的这篇帖子说的很清楚了啊,该不是.ini文件不知怎么写吧?


新建一个文本文件,将后缀改为.ini。

INI文件格式如下:
[Database] Database为Section
server=wlq server,database,uid,pwd为Key
database=mydatabase
uid=sa
pwd=123456

Kevin2328 2008-12-22
  • 打赏
  • 举报
回复
都很清楚了,哪里不明白的?


Private Sub CheckBox1_Click()
If CheckBox1.Value Then
If NoIniFile Then
SpecifyIni "C:\INI.ini"
End If

If Not WriteString("Section1", "Key1", CheckBox1.Caption) Then
MsgBox ErrorMsg
End If
End If
End Sub

'INI.ini/Section1/Key1
Private Sub Form_Load()
Dim strTest As String
If NoIniFile Then
SpecifyIni "C:\INI.ini"
End If

strTest = ReadString("Section1", "Key1", 256)

If strTest <> "" Then
ComboBox1.Text = strTest
CheckBox1.Value = 1
Else
ComboBox1.Text = "蘇饜离"
CheckBox1.Value = 0
End If

End Sub

1,488

社区成员

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

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