我用ADO连SQL数据库,我现在想写个INI文件,每次连数据库都读这个INI文件,不知道该如何实现(INI文件的内容和VB中如何调用它),还望指教,谢谢!
解决方案 »
- 如何获取对话框的句柄?
- 不小心把EXCEL的menu删了,怎么办?
- vb问题 如何把MSHFlexGrid表格中的NxM行的数据写到txt文件里?
- 求InstallShield Developer 7.0的东方语言包
- 我想实现网上注册功能该怎么做呢???(用软件方法解决)用什么样的算法请给于提示
- 如果执行亿万次运算,怎么样提高速度?
- 请教当窗体LOAD时,我要改变该窗体中Adodc1的RECORDSOURCE,怎么改???
- asp:
- 怎样用SQL在某条记录后添加记录?
- vb.net的timer
- 为什么我用datareport完成了的报表,运行后老是提示"报表宽度大于纸的宽度",请指教
- DELPHI里很容易解决,VB里就这么难吗?在VSFlexGrid表格里画线问题(DrawCell里写代码)。
Option Explicit
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文件进行读操作
'参数一:文件路径
'参数二:条目的小节名称
'参数三:项名或条目名
Function GetProfileString(StrFileName As String, StrAppName As String, StrKeyName As String) As String
GetProfileString = String(255, 0)
GetPrivateProfileString StrAppName, StrKeyName, "", GetProfileString, 255, StrFileName
GetProfileString = Left(GetProfileString, InStr(GetProfileString, Chr(0)) - 1)
End Function'功能简介:对INI文件进行写操作
'参数一:文件路径
'参数二:条目的小节名称
'参数三:项名或条目名
'参数四:写操作字符串
Function WriteProfilestring(StrFileName As String, StrAppName As String, StrKeyName As String, StrWrite As String) As Boolean
On Error GoTo WriteErr
WritePrivateProfileString StrAppName, StrKeyName, StrWrite, StrFileName
WriteProfilestring = True
Exit Function
WriteErr:
End Function
Option Explicit
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文件进行读操作
'参数一:文件路径
'参数二:条目的小节名称
'参数三:项名或条目名
Function GetProfileString(StrFileName As String, StrAppName As String, StrKeyName As String) As String
GetProfileString = String(255, 0)
GetPrivateProfileString StrAppName, StrKeyName, "", GetProfileString, 255, StrFileName
GetProfileString = Left(GetProfileString, InStr(GetProfileString, Chr(0)) - 1)
End Function'功能简介:对INI文件进行写操作
'参数一:文件路径
'参数二:条目的小节名称
'参数三:项名或条目名
'参数四:写操作字符串
Function WriteProfilestring(StrFileName As String, StrAppName As String, StrKeyName As String, StrWrite As String) As Boolean
On Error GoTo WriteErr
WritePrivateProfileString StrAppName, StrKeyName, StrWrite, StrFileName
WriteProfilestring = True
Exit Function
WriteErr:
End Function
String,ByVal lpDefault As String,ByVal lpRetrm-String As String,ByVal cbReturnString As Integer, _
ByVal Filename As String)As Integer
Public Declare Function GetPrivatePfileInt Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As String, _
ByVal lpDefault As Integer,ByVal Filename As String)As Integer
Public Declare Funciton WritePrivateprofileString Lib“Kernel”(ByVal lpApplicationName As String, _
ByVal lpKeyName As String,ByVal lpString As String,ByVal lplFileName As String)As Integer
Public Function GetIniS(ByVal SectionName As String,ByValKeyWord As String,ByVal DefString As String)As String
Dim ResultString As String*144,Temp As Integer
Dim s As String,i As Integer
Temp%=GetPrivateProfileString(SectionName,KeyWord,"",ResultString,144,AppProfileName())
’检索关键词的值
If Temp%>0 Then ’关键词的值不为空
s=""
For i=1 To 144
If Asc(Mid$(ResultString,I,1))=0 Then
ExitFor
Else
s=s & Mid$(ResultString,I,1)
End If
Next
Else
Temp%=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName())
’将缺省值写入INI文件
s=DefString
End If
GetIniS=s
End FunctionPublic Function GetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefValue As Ineger)As Integer
Dim d As Long,s As String
d=DefValue
GetIniN=GetPrivateProfileInt(SectionName,
KeyWord,DefValue,ppProfileName())
If d<>DefValue Then
s=""
d=WritePrivateProfileString(SectionName,
KeyWord,s,AppProfileName())
End If
End FunctionPublic Sub SetIniS(ByVal SectionName As String,BtVal KeyWord As String,ByVal ValStr As String)
Dim res%
res%=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName())
End Sub
Public Sub SetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal ValInt As Integer)
Dim res%,s$
s$=Str$(ValInt)
res%=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName())
End SubSectionName为每一部分的标题,KeyWord为关键词,GetIniS和GetIniN中的DefValue为关键词的缺省值,SetIniS和SetIniN的ValStr和ValInt为要写入INI文件的关键词的值。
Option Explicit'declares for ini controlling
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 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 WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, 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'when form is loaded
Private Sub Form_Load()'if error occures resume still
On Error Resume Next'local variables
Dim File As String, OFLen As Double, _
Str As String'set our varibles
File = "C:\temp.txt"
OFLen = FileLen(File)'write few example sections:
WriteIniSection File, "Test1", ""
WriteIniSection File, "Test2", "Here shoud be found some text"'write few ini strings
WriteIni File, "Test3", "Ini1", "This is ini 1"
WriteIni File, "Test1", "Ini2", "This is ini 2"'inform we're written the data
MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)'read the ini file
Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf'show data
MsgBox Str'end application
EndEnd Sub'// INI CONTROLLING PROCEDURES'reads ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v - 1)
End Function'reads ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v - 1)
End Function'writes ini
Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub'writes ini section
Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
WritePrivateProfileSection Section, Value, Filename
End Sub
Dim strSectionTemp As String
Dim strNameTemp As String
Dim strreturn As String
Global strPath As String
'------------------------INI文件的操作---------------------------
Function setProfile(strFileName As String, strSection As String, strName As String, strSave As String) As Boolean
Dim strTemp As String
Dim strfileback As String
Dim strreturn As String
strfileback = App.Path & "\ini\tmp.tmp" '临时文件是用来存放中转信息的
Open strFileName For Input As #1
Open strfileback For Output As #2
Do While Not EOF(1)
Line Input #1, strTemp
strreturn = strTemp
Print #2, strreturn
If InStr(1, Trim(strTemp), "[") <> 0 Then
If InStr(1, Trim(strTemp), Trim(strSection)) <> 0 Then
Do While Not EOF(1)
Line Input #1, strTemp
If InStr(1, Trim(strTemp), Trim(strName)) <> 0 Then Exit Do '找到所要修改的字段值
strreturn = strTemp
Print #2, strreturn '拷贝不需要的字段值
Loop
strreturn = strName & "=" & strSave '修改
Print #2, strreturn
End If
End If
Loop
Close #1
Close #2
Open strfileback For Input As #1
Open strFileName For Output As #2
Do While Not EOF(1) And EOF(2)
Line Input #1, strreturn
Print #2, strreturn
Loop
Close #1
Close #2
End FunctionFunction GetProfile(strFileName As String, strSection As String, strName As String) As String
'这个函数是用来对INI文件进行读操作的
'函数说明:
'strFileName 是所要读取的文件名
'strSection 是这个文件中的一个节点名
'strName 是所要查找的字段名
strSectionTemp = ""
strNameTemp = ""
strreturn = ""
On Error GoTo ErrSrchSection
Open strFileName For Input As #1
' 下面这段程序是用来查找节点的
Do While Not EOF(1)
strCharA = Input(1, #1)
If strCharA = "[" Then
Do While Not EOF(1)
strCharB = Input(1, #1)
If strCharB = "]" Then Exit Do
strSectionTemp = strSectionTemp & strCharB
Loop
End If
If strSectionTemp = strSection Then
strCharA = Input(2, #1)
Exit Do
Else
strSectionTemp = ""
End If
Loop
On Error GoTo ErrReadFile
aa:
'下面这段程序是用来查找所要查找的字段的
strNameTemp = ""
Do While Not EOF(1)
strCharA = Input(1, #1)
If strCharA <> "=" Then
strNameTemp = strNameTemp & strCharA '得到名称
Else
Exit Do
End If
Loop
If strNameTemp = strName Then
Line Input #1, strreturn '如果找到与它匹配的字段名,就返回得到的值
Else
Line Input #1, strreturn '如果未找到与它匹配的字段名,就继续找
GoTo aa
End If
Close #1
GetProfile = strreturn
Exit Function
ErrReadFile:
Dim inrRet As Integer
intret = MsgBox("在文件中没有找到所要查找的字段", vbAbortRetryIgnore, "错误信息")
Select Case intret
Case vbAbort
GetProfile = ""
Close #1
Exit Function
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
ErrSrchSection:
MsgBox "节点未找到", vbOKOnly
GetProfile = ""
Close #1
End Function
'end------------------INI文件的操作------------------
'---------------------程序中调用,保存部分---------
strPath = App.Path & "\ini\prnset.ini"
setProfile strPath, "存根", "科目.X", T0x
'---------------------程序中调用,调用部分---------'---------------------------INI配置读取-----------------
strPath = App.Path & "\ini\prnset.ini"T0x = GetProfile(strPath, "存根", "科目.X")