|
|
#2
fairy42008-01-28 15:10
'聲明INI配置檔讀寫API函數
Private 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 Int32, ByVal lpFileName As String) As Int32 Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32 '定義讀取配置檔函數 Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String Dim Str As String = LSet(Str, 256) GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName) Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1) End Function '定義寫入配置檔函數 Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName) End Function '寫入 Try Dim path As String path = Application.StartupPath + "\server.ini" WriteINI("Server", "IP", TextBox1.Text, path) WriteINI("Server", "port", TextBox2.Text, path) WriteINI("Server", "userName", TextBox3.Text, path) 'WriteINI("Server1", "IP", TextBox1.Text, path) 'WriteINI("Server1", "port", TextBox2.Text, path) 'WriteINI("Server1", "userName", TextBox3.Text, path) MsgBox("配置設置已經成功!!!!") Me.Close() Catch ex As Exception MsgBox("錯誤!!!!") End Try '讀取 Dim path As String path = Application.StartupPath + "\server.ini" TextBox1.Text = GetINI("Server", "IP", "", path) TextBox2.Text = GetINI("Server", "port", "", path) TextBox3.Text = GetINI("server", "userName", "", path) |
我是一个新手,很多方面还不是很明白,读取ini也是刚刚接触而已,在网上查找了很多,都是读取一段的,有没有能读取不同段的代码??给个示范就可以了。
比如单段的就是
[xxx001]
xxx001=xxx001
xxx002=xxx002
我要的多段读取就是:
[xxx001]第一段
xxx001=xxx001
xxx002=xxx002
[yyy001]第2段
yyy001=yyy001
yyy002=yyy002
............
以此类推的!
在代码中如何区别段和段?
能用实例代码演示一下吗?谢谢!
[[it] 本帖最后由 fnp902003 于 2008-1-28 14:23 编辑 [/it]]
