1,488
社区成员
发帖
与我相关
我的任务
分享
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
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
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