你的问题就是如何操作ini文件的方法,反文件改成.ini文件,就成了。VB操作.ini后缀文件的方法读文件用到GetPrivateProfileString,写文件需要用到WritePrivateProfileString。 在窗体放置两个命令按钮Command1与Command2,分别用来执行写操作与读操作。 Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfile StringA" (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 Private Sub Command1_Click() Dim A As Long '写信息 '修改ABC.INI文件中TIP字段中START的值为当前系统时间 '如果该文件不存在会自动建立,当函数返回值为0时说明修改不成功 A = WritePrivateProfileString("TIP", "START", Time$, App.Path & "\ABC.INI") If A = 0 Then MsgBox ("写文件时出错") End Sub Private Sub Command2_Click() Dim A As Long Dim T As String '读取信息 T = Space$(1000) '事先定义读取值的字串宽度 '读取ABC.INI文件中TIP字段中START的值并打印出来 '当函数返回值为0时说明读取数据出错 A = GetPrivateProfileString("TIP", "START", "", T, 1000, App.Path & "\ABC.INI") If A = 0 Then MsgBox "找不到所需字段": Exit Sub Print Left$(T, Len(Trim$(T)) - 1) End Sub
'属性: ' File 就是要操作的ini文件名 '方法: ' SaveSetting "域名","键名","键值" -> 保存键值 ' GetSetting "域名","键名" -> 返回键值 ' GetSection "域名",""键值数组名" -> 批量取得键值 ' DeleteSection "域名" -> 删除一个域 '----------------------------------- ' Ini File Functions Class ' Copyright (C) 1996, Jens Balchen ' Uses ' Exposes ' Function GetSetting ' Function SaveSetting ' Function GetSection ' Comments '-- Option Explicit ' Property for file to read Public File As String 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 Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As LongSub DeleteSection(ByVal Section As String) Dim retval As Long retval = WritePrivateProfileString(Section, 0&, "", File) End SubPublic Function SaveSetting(ByVal Section As String, ByVal Key As String, ByVal Value As String) Dim retval As Long SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File) End FunctionPublic Function GetSetting(ByVal Section As String, ByVal KeyName As String, Optional ByVal Default As Variant = "") As String Dim retval As Long Dim t As String * 255 Dim ts As String ' Get the value retval = GetPrivateProfileString(Section, KeyName, "", t, Len(t), File) ' If there is one, return it If retval > 0 Then 'ts = StrConv(LeftB(StrConv(t, vbFromUnicode), retval), vbFromUnicode) ts = Left$(t, retval) ts = Replace(ts, vbNullChar, "") Else ts = Default End If GetSetting = ts End FunctionPublic Function GetSection(ByVal Section As String, KeyArray() As String) As Long Dim retval As Long ' Allocate space for return value Dim t As String * 2500 Dim lastpointer As Long Dim nullpointer As Long Dim ArrayCount As Long Dim keystring As String ReDim KeyArray(0) ' Get the value retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File) ' If there is one, return it If retval > 0 Then ' Separate the keys and store them in the array nullpointer = InStr(t, Chr$(0)) lastpointer = 1 Do While (nullpointer <> 0 And nullpointer > lastpointer + 1) ' Extract key string keystring = Mid$(t, lastpointer, nullpointer - lastpointer) ' Now add to array ArrayCount = ArrayCount + 1 ReDim Preserve KeyArray(ArrayCount) KeyArray(ArrayCount) = keystring ' Find next null lastpointer = nullpointer + 1 nullpointer = InStr(nullpointer + 1, t, Chr$(0)) Loop End If ' Return the number of array elements GetSection = ArrayCount End Function
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString 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 lplFileName As String) As Long Public Function GetINI(AppName As String, KeyName As String, FileName As String) As String Dim RetStr As String RetStr = String(255, Chr(0)) GetINI = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", RetStr, Len(RetStr), FileName)) End FunctionPublic Function WriteIni(AppName As String, KeyName As String, InString As String, FilePath As String) As String Dim r As Integer r = WritePrivateProfileString(AppName, KeyName, InString, FilePath) End Function 操作代码""""ini的写操作TEXT2.text 是的后边的值Private Sub Write_Click() Call WriteIni("Database", "ServerName", Text2.Text, "d:\AAA.ini")End Sub'ini的读操作........ 得到ServerName的值Private Sub Read_Click() Text2.Text = GetINI("Database" , "ServerName", "d:\a.ini") End Sub '其它的字段和这个是一样的,自己添加就可以了,hoho!!
to nhyjk(死去活来) ( ) 51365133(渊海)不是都说了吗?你还有什么不明白的?
“各位,我不想你们把代码粘贴就完事,我想得到符合我的解决办法! 再谢各位了!!!”这种精神值得学习!我提供一点我的思路,不知是否合适,见笑了用 instr 函数得到 “=” 在字符串中的位置, 然后用mid 或 right 函数取子串
我有一个数据库连接文件AAA.net,内容如下: [Database] ServerName=AA database=DB LogId=sa LogPassword=请问我怎样读取里面等号后面的字符,并赋值到程序对应的变量中? 谢谢!!!!!Open "AAA.net" For Input As #1 Dim TextLine As String Dim strArr() As String Dim ServerName As String Dim Database As String Dim LogId As String Dim LogPassword As StringDo Until EOF(1) Line Input #1, TextLine If Instr(TextLine,"[") = 0 Then strArr = Split(Text, "=") Select Case strArr(0) Case "ServerName" ServerName = strArr(1) Case "Database" Database = strArr(1) Case "LogId" LogId = strArr(1) Case "LogPassword" LogPassword = strArr(1) End Select End If Loop
在窗体放置两个命令按钮Command1与Command2,分别用来执行写操作与读操作。
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfile
StringA" (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
Private Sub Command1_Click()
Dim A As Long
'写信息
'修改ABC.INI文件中TIP字段中START的值为当前系统时间
'如果该文件不存在会自动建立,当函数返回值为0时说明修改不成功
A = WritePrivateProfileString("TIP", "START", Time$, App.Path & "\ABC.INI")
If A = 0 Then MsgBox ("写文件时出错")
End Sub
Private Sub Command2_Click()
Dim A As Long
Dim T As String
'读取信息
T = Space$(1000) '事先定义读取值的字串宽度
'读取ABC.INI文件中TIP字段中START的值并打印出来
'当函数返回值为0时说明读取数据出错
A = GetPrivateProfileString("TIP", "START", "", T, 1000, App.Path & "\ABC.INI")
If A = 0 Then MsgBox "找不到所需字段": Exit Sub
Print Left$(T, Len(Trim$(T)) - 1)
End Sub
' File 就是要操作的ini文件名
'方法:
' SaveSetting "域名","键名","键值" -> 保存键值
' GetSetting "域名","键名" -> 返回键值
' GetSection "域名",""键值数组名" -> 批量取得键值
' DeleteSection "域名" -> 删除一个域
'-----------------------------------
' Ini File Functions Class
' Copyright (C) 1996, Jens Balchen
' Uses
' Exposes
' Function GetSetting
' Function SaveSetting
' Function GetSection
' Comments
'--
Option Explicit
' Property for file to read
Public File As String
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 Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As LongSub DeleteSection(ByVal Section As String)
Dim retval As Long
retval = WritePrivateProfileString(Section, 0&, "", File)
End SubPublic Function SaveSetting(ByVal Section As String, ByVal Key As String, ByVal Value As String)
Dim retval As Long
SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)
End FunctionPublic Function GetSetting(ByVal Section As String, ByVal KeyName As String, Optional ByVal Default As Variant = "") As String
Dim retval As Long
Dim t As String * 255
Dim ts As String
' Get the value
retval = GetPrivateProfileString(Section, KeyName, "", t, Len(t), File)
' If there is one, return it
If retval > 0 Then
'ts = StrConv(LeftB(StrConv(t, vbFromUnicode), retval), vbFromUnicode)
ts = Left$(t, retval)
ts = Replace(ts, vbNullChar, "")
Else
ts = Default
End If GetSetting = ts
End FunctionPublic Function GetSection(ByVal Section As String, KeyArray() As String) As Long
Dim retval As Long
' Allocate space for return value
Dim t As String * 2500
Dim lastpointer As Long
Dim nullpointer As Long
Dim ArrayCount As Long
Dim keystring As String
ReDim KeyArray(0)
' Get the value
retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File)
' If there is one, return it
If retval > 0 Then
' Separate the keys and store them in the array
nullpointer = InStr(t, Chr$(0))
lastpointer = 1
Do While (nullpointer <> 0 And nullpointer > lastpointer + 1)
' Extract key string
keystring = Mid$(t, lastpointer, nullpointer - lastpointer)
' Now add to array
ArrayCount = ArrayCount + 1
ReDim Preserve KeyArray(ArrayCount)
KeyArray(ArrayCount) = keystring
' Find next null
lastpointer = nullpointer + 1
nullpointer = InStr(nullpointer + 1, t, Chr$(0))
Loop
End If
' Return the number of array elements
GetSection = ArrayCount
End Function
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString 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 lplFileName As String) As Long
Public Function GetINI(AppName As String, KeyName As String, FileName As String) As String
Dim RetStr As String
RetStr = String(255, Chr(0))
GetINI = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", RetStr, Len(RetStr), FileName))
End FunctionPublic Function WriteIni(AppName As String, KeyName As String, InString As String, FilePath As String) As String
Dim r As Integer
r = WritePrivateProfileString(AppName, KeyName, InString, FilePath)
End Function
操作代码""""ini的写操作TEXT2.text 是的后边的值Private Sub Write_Click()
Call WriteIni("Database", "ServerName", Text2.Text, "d:\AAA.ini")End Sub'ini的读操作........
得到ServerName的值Private Sub Read_Click()
Text2.Text = GetINI("Database" , "ServerName", "d:\a.ini")
End Sub
'其它的字段和这个是一样的,自己添加就可以了,hoho!!
再谢各位了!!!”这种精神值得学习!我提供一点我的思路,不知是否合适,见笑了用 instr 函数得到 “=” 在字符串中的位置,
然后用mid 或 right 函数取子串
[Database]
ServerName=AA
database=DB
LogId=sa
LogPassword=请问我怎样读取里面等号后面的字符,并赋值到程序对应的变量中?
谢谢!!!!!Open "AAA.net" For Input As #1
Dim TextLine As String
Dim strArr() As String
Dim ServerName As String
Dim Database As String
Dim LogId As String
Dim LogPassword As StringDo Until EOF(1)
Line Input #1, TextLine
If Instr(TextLine,"[") = 0 Then
strArr = Split(Text, "=")
Select Case strArr(0)
Case "ServerName"
ServerName = strArr(1)
Case "Database"
Database = strArr(1)
Case "LogId"
LogId = strArr(1)
Case "LogPassword"
LogPassword = strArr(1)
End Select
End If
Loop