我查看了下配置文件
开头为【】形式的,称Section,现在不知道怎么生成ini格式的文件,并命名,并且怎么初始话里面的段这个信息,是手动创建的吗?
读写文件我大概知道用下面的API函数
GetPrivateProfileInt
GetPrivateProfileString
WritePrivateProfileString
开头为【】形式的,称Section,现在不知道怎么生成ini格式的文件,并命名,并且怎么初始话里面的段这个信息,是手动创建的吗?
读写文件我大概知道用下面的API函数
GetPrivateProfileInt
GetPrivateProfileString
WritePrivateProfileString
解决方案 »
- Microsoft ActiveX Data Objects Recordset 2.8Library的问题
- 在记事本中都能输入的特殊字符,在VB中变成了“?”号?
- 哪位仁兄有rar的解压控件?用shell解压太不稳定.
- VB种的TreeView问题~~~~!
- 知道FORM的名字字符如"form1",如何执行这个窗体。
- 两个数据库之间能不能实现多个表之间的交互查询?
- 求media player歌词显示控件~
- 关于报表打印问题
- 关于TextBox的一个有意思的现象:(关键字:TextBox,MultiLine,KeyCode)
- 替我看看下面关于winsock的代码错在哪里?急!!!
- 新手紧急求教~~
- 为什么我的Hscroll拖动后停下来会闪动呢
keyword1=valuel
keyword2=value2
……
[section2]
keyword1=value1
keyword2=value2
……可以手动创建一个初始ini文件,再在程序里面读取或修改。
网上搜索示例一大把
生成:
open "name.ini"for output as #1
print #1,"【】1111:" & 设置1的值
print #1,"【】2222:" & 设置2的值
....
....
close #1
读取:
dim m$
open "name.ini" for input as #2
do while not eof(1)
line input #!,m
if instr(m,"【】1111:")=1 then 设置1=right(m,len(m)-7)
if instr(m,"【】2222:")=1 then 设置2=right(m,len(m)-7)
....
....
close #2
s = Space(256)
GetPrivateProfileString "小节名", _
"关键字", "", s, 1024, "配置文件绝对路径"
写ini
WritePrivateProfileString "小节名", "关键字", "关键字的值", "配置文件绝对路径"
急...在线等!ini文件如何在不知道有多少个key的情况下,读取某小节中的全部内容.
"WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal _
lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As BooleanPrivate Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal _
lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString _
As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSectionNames _
Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" _
(ByVal lpReturnBuffer As String, ByVal nSize As Long, _
ByVal lpName 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 lpName As String) As Long
Private sIniPath As String
Private sIniFile As String
Private AppPath As String
Private Sub Class_Initialize()
Dim iCount As Integer
Dim sKeyValue As String * 255
Dim sTmpStr As String
If InStr(UCase$(App.Path), "PROJECTS") = 0 Then
AppPath = App.Path
Else 'to prevent error when running in development situation
AppPath = Left$(App.Path, Len(App.Path) - 9)
End If
sIniFile = "\PCPOS.INI"
If Dir(AppPath & sIniFile) = "" Then
' if local .ini file not found, create it
WritePrivateProfileString "General", "IniFilePath", "Current", AppPath & sIniFile
WritePrivateProfileString "General", "DataPath", AppPath & "\DATA", AppPath & sIniFile
WritePrivateProfileString "Initialize", "bModemCable", "True", AppPath & sIniFile
' WritePrivateProfileString "Terminal", "SalesDataPath", AppPath & "\DATA", AppPath & sIniFile
' WritePrivateProfileString "Terminal", "Terminal", "0", AppPath & sIniFile
sIniPath = AppPath
Exit Sub
End If
iCount = GetPrivateProfileString("General", "IniFilePath", NULL_KEY_VALUE, sKeyValue, Len(sKeyValue), AppPath & sIniFile)
If iCount = 0 Then
Err.Raise vbObjectError + 1001, "Read INI file failed"
Exit Sub
End If
sTmpStr = Left$(sKeyValue, InStr(sKeyValue, Chr(0)) - 1)
If sTmpStr = NULL_KEY_VALUE Or sTmpStr = "" Then
Err.Raise vbObjectError + 1001, "Invalid INI file path"
Exit Sub
End If
If sTmpStr = "Current" Then
sIniPath = AppPath
Else
If Dir(sTmpStr & sIniFile) = "" Then
Err.Raise vbObjectError + 1001, "Invalid INI file path"
Exit Sub
End If
sIniPath = sTmpStr
End If
End SubPublic Function ReadKey(VsECTION As String, vKeyName As String) As String
Dim iCount As Integer
Dim sKeyValue As String * 255
iCount = GetPrivateProfileString(VsECTION, vKeyName, NULL_KEY_VALUE, sKeyValue, Len(sKeyValue), sIniPath & sIniFile)
If iCount = 0 Then
Err.Raise vbObjectError + 1001, "Reading INI file failed"
Exit Function
End If
ReadKey = Left$(sKeyValue, InStr(sKeyValue, Chr(0)) - 1)
End FunctionPublic Sub WriteKey(VsECTION As String, vKeyName As String, Optional vKeyValue As String = "")
WritePrivateProfileString VsECTION, vKeyName, vKeyValue, sIniPath & sIniFile
End SubPublic Property Get InitPath() As String
InitPath = sIniPath
End PropertyPublic Function ReadGridSetting(VsECTION As String, vKeyName As String) As String
Dim sCfgFile As StringDim iCount As Integer
Dim sKeyValue As String * 255
sCfgFile = sIniPath & "\GridSetting.cfg"
If Dir(sCfgFile) = "" Then 'if not found
WritePrivateProfileString "Test", "Test", "Test", sCfgFile
End If
iCount = GetPrivateProfileString(VsECTION, vKeyName, NULL_KEY_VALUE, sKeyValue, Len(sKeyValue), sCfgFile)
If iCount = 0 Then
ReadGridSetting = NULL_KEY_VALUE
Exit Function
End If
ReadGridSetting = Left$(sKeyValue, InStr(sKeyValue, Chr(0)) - 1)End FunctionPublic Sub WriteGridSetting(VsECTION As String, vKeyName As String, Optional vKeyValue As String = "")
Dim sCfgFile As String sCfgFile = sIniPath & "\GridSetting.cfg" WritePrivateProfileString VsECTION, vKeyName, vKeyValue, sCfgFile
End SubPublic Function GetAllSections() As Variant
Dim lngSize As Long
Dim astrItems As Variant
Dim i As Integer
Dim astrSections As Variant
Dim sMasterSource As String
Dim strSections As String
On Error GoTo HandleErrorslngSize = 1024
Do
strSections = Space$(lngSize)
lngSize = GetPrivateProfileSectionNames(strSections, lngSize, AppPath & sIniFile)
If lngSize = 0 Then
GoTo ExitHere
ElseIf lngSize = Len(strSections) - 2 Then
lngSize = lngSize * 2
Else
strSections = Left$(strSections, lngSize - 1)
Exit Do
End If
Loop
astrSections = Split(strSections, vbNullChar)
GetAllSections = astrSections
ExitHere:
Exit Function
HandleErrors:
Err.Raise Err.Number, Err.Source, Err.Description
End FunctionPublic Function GetSingleSection(ByVal pSectionName As String) As Variant
Dim strSection As String
Dim lngSize As Long
Dim astrSections As Variant
Dim i As Integer
On Error GoTo HandleErrors
lngSize = 1024
Do
strSection = Space$(lngSize)
lngSize = GetPrivateProfileSection(pSectionName, strSection, lngSize, AppPath & sIniFile)
If lngSize = 0 Then
GoTo ExitHere
ElseIf lngSize = Len(strSection) - 2 Then
lngSize = lngSize * 2
Else
strSection = Left$(strSection, lngSize - 1)
Exit Do
End If
Loop
astrSections = Split(strSection, vbNullChar)
GetSingleSection = astrSectionsExitHere:
Exit Function
HandleErrors:
Err.Raise Err.Number, Err.Source, Err.Description
End Function