看了一些VB ini文件写入读取的方法 但是还是不知道怎么写,上面写了很长 很多,但是我不知道怎么用,我想问一下怎样写才能实现在TEXT1中写字,然后点Command1提交,然后TEXT1里面的数字就写进INI里面[Section]的Key1=里面,然后怎么在Combo1的下拉菜单里面能看到key1,然后我点击Command2删除的时候能删掉key1
解决方案 »
- 运行代码时,出现下面的错误,请高手解决一下
- Listbox 里面列表怎么样才能显示成这样 在线等~~~~
- 谁能解决这个问题vb捕捉动态分配内存的,100分全给先谢谢了
- 请各位大侠帮忙修改一下vb连接公网ip下的sql2000数据库的字符串,谢谢!
- 数组调用超难问题
- txt
- 问什么我的vb不支持鼠标滚轮?
- 为什么RECORDSET.RECORDCOUNT=-1,为什么不能确定记录数?
- 急急如律令!!!VB大虾。。。。。帮帮忙
- 关于mshflexgrid的问题
- 通过 Microsoft.XMLHTTP 有办法获取 Bing Translator 的结果吗?
- 只会 VB6.0,想写手机软件。该学什么?
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal w_returnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long'----------------------------------------------------
'------------從INI文件中取得數據---------------------
'---設計者:,設計日期:2004/04/07----------------
'----------------------------------------------------
Public Function GetIni(ByVal KeyName As String, Optional ByVal KeyGroup As String, Optional ByVal PathName As String) As Variant
Dim strKeyGroup As String, strPathName As String, strRtn As String, nSize As Long
strRtn = Space(255)
strKeyGroup = IIf(KeyGroup = "", "Setup", KeyGroup)
If PathName = "" Then
strPathName = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "Mcs.ini"
Else
strPathName = PathName
End If
nSize = GetPrivateProfileString(strKeyGroup, KeyName, "", strRtn, Len(strRtn), strPathName)
If nSize > 0 Then
GetIni = Left(strRtn, nSize)
End If
End Function'----------------------------------------------------
'------------寫入數據到INI文件中---------------------
'---設計者:,設計日期:2004/04/07----------------
'----------------------------------------------------
Public Function SetIni(ByVal KeyValue As String, ByVal KeyName As String, Optional ByVal KeyGroup As String, Optional ByVal PathName As String) As Long
Dim strKeyGroup As String, strPathName As String
strKeyGroup = IIf(KeyGroup = "", "Setup", KeyGroup)
If PathName = "" Then
strPathName = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "Mcs.ini"
Else
strPathName = PathName
End If
SetIni = WritePrivateProfileString(strKeyGroup, KeyName, KeyValue, strPathName)
End Function
读:
DIM strRtn as string
strRtn=string(255,0)
call GetPrivateProfileString("AAA","BBB", "123", strRtn, 256, “c:/TEST.TXT”)
写:call WritePrivateProfileString("AAA","BBB", “123”, “c:/TEST.TXT”)
C盘下有个TEST.TXT,里面的内容:[AAA]
BBB=123
假如文件C:\Data.ini
裡面內容
[Setting]
Key1=Value1
getini函數
Keyname:表示Key1,KeyGroup:表示Setting.PathName:c:\Data.ini
返回值:Value1
setini函數
KeyName:表示Key1,KeyGroup:表示Setting,PathName:c:\data.ini,KeyValue:表示Value1
按liguicd的方法这样定义对吗?还是不用定义的啊 不用的话 没反应啊~
Public 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
liguicd上面的
赋了什么值给strRtn变量啊,
call GetPrivateProfileString("AAA","BBB", "123", strRtn, 256, “c:/TEST.TXT”) 括号里面strRtn, 256,都是什么意思啊?我能不能这样写啊:
DIM strRtn as string
strRtn=string(255,0)
Private Sub Command1_Click()
call GetPrivateProfileString("AAA","BBB", "123", strRtn, 256, “c:/TEST.TXT”)
End Sub
Private Sub Command2_Click()
call WritePrivateProfileString("AAA","BBB", “123”, “c:/TEST.TXT”)
End Sub
Private Sub Command1_Click()
Public Function SetIni(ByVal KeyValue As String, ByVal KeyName As String, Optional ByVal KeyGroup As String, Optional ByVal PathName As String) As Long
Dim strKeyGroup As String, strPathName As String
strKeyGroup = IIf("c:\Data.ini" = "", "Setup", "c:\Data.ini")
If Key1 = "text1" Then
strKey1 = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "Mcs.ini"
Else
strKey1 = Key1
End If
SetIni = WritePrivateProfileString(strKeyGroup, KeyName, KeyValue, strPathName)
End Function
End Sub他老是说我编译错误是什么意思啊?
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
提示:常数、固定长度字节串、数组、用户定义类型以及Declare语句不允许作为对象模块的Public成员。
上次我看人家例子的时候复制进去也出现这样的 但是后来我修改了一下 又不出错了,什么原因呢?
然後直接調整這兩個函數來操作INI文件:
setini 表示寫值到ini中。
getini 表示從ini讀值。假如文件C:\Data.ini
裡面內容
[Setting]
Key1=Value1
getini函數
Keyname:表示Key1,KeyGroup:表示Setting.PathName:c:\Data.ini
返回值:Value1
setini函數
KeyName:表示Key1,KeyGroup:表示Setting,PathName:c:\data.ini,KeyValue:表示Value1如果還不明白,那我也沒辦法了,那你給我一個郵箱,我再寫個例子發給你了
http://download.csdn.net/source/1165567纯粹用注册表其实比较少的,ini文件便于修改,而且整个应用程序目录移动时也可以一并转移。
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
H_WindowPosition=[426,266,426,266,0]
[SysParam]
RemPass=1
ServerCount=2
server0=127.0.0.1
UserCount=1答:^\s*(\[.*\])\s*$关于ini操作的工程源码:http://www.symental.com/sfw/IniExample.rar
就是 我通过一个下拉菜单Combo1 可以读出INI文件里面[Setting]中的Key1,能不能实现我选择菜单中的Key1时,TEXT2里面显示出的值Value1啊!???
你的这个INI操作的工程源码很好,但是读取的时候怎么都是弹出窗口来显示读出的数据的啊~我看下先 应该点读取时可以让他在TEXT里面显示吧!? 我看下先,不懂再请教您!
ini文件简单明了些
例子说明:添加2个按钮,1个文本框,可以先点Command2Option Explicit
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 Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal w_returnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPrivate Sub Command1_Click()
Dim strRtn As String
strRtn = Space(256)
Dim ret As Long
ret = GetPrivateProfileString("AAA", "BBB", "", strRtn, 256, "d:/a.TXT")
text1.Text = Left(strRtn, ret)
End Sub
Private Sub Command2_Click()
Call WritePrivateProfileString("AAA", "BBB", text1.Text, "d:/a.TXT")
End Sub
以前readini有别人写好的固定模块的,是查找末尾字符的
今天受你影响看了这个API,既然有长度传出别人写的为什么还要查末尾字符,不稳定,而且麻烦
于是改进了下,原因:vb识别中文字符长度不是实际占用的长度
vb调用其他的API也都可以这么改进一下的
呵呵,帮助别人学习的时候也是帮助自己学习,这句话倒是真的蛮实在的text1.Text =StrConv(Left(StrConv(strRtn, vbUnicode), ret), vbFromUnicode)
Dim arr As Variant ' 用 Variant,读取的时候就省事了灵活性比较大
ReDim arr(10) ' 0 到 10 存什么自己规定
arr(0) = "c:\123456"
arr(1) = 300&
arr(2) = "某些信息"
' arr(3)=........... Open app.path & "\arr.dll" For Binary As #1 '伪装成dll,一般用户不敢乱改的
Put #1, , arr
Close #1
End SubSub getarr()
Dim arr As Variant
Open app.path & "\arr.dll" For Binary As #1
Get #1, , arr MsgBox Join(arr, " ")
arr(2) = "修改某些信息"
Put #1, 1, arr '重新写回去
Close #1
End Sub
您改的是这里吗?
text1.Text = Left(strRtn, ret)
改成
text1.Text =StrConv(Left(StrConv(strRtn, vbUnicode), ret), vbFromUnicode)
谢谢 您的帮助~
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 LongPrivate Sub Form_Load()
getItem "d:\Option.ini"
End SubPrivate Sub getItem(inifile As String)
Dim myStr As String
Dim ret As Long
Dim retDevice
myStr = Space(Bufsize)
ret = GetPrivateProfileString("psection", vbNullString, "", myStr, Bufsize, inifile)
If ret > 0 Then
myStr = Left(myStr, ret)
retDevice = Split(myStr, Chr(0), , vbBinaryCompare)
End IfIf UBound(retDevice) > 0 Then
For i = 0 To UBound(retDevice) - 1
Combo1.AddItem retDevice(i)
Next i
End IfEnd Sub
这段源码可以在下拉菜单Combo1中读出INI文件里面的数据
==========================================================================================================================================================================
Option ExplicitPrivate Const BOUNDARY$ = vbNullChar & vbNullCharPrivate 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
Private strINIfile As String Private Function EnumName(ByVal SectionName As String, ByVal INI_FileName As String) As String Dim strBuff As String * 512
Call GetPrivateProfileString(SectionName, 0&, "", strBuff, 500, INI_FileName)
EnumName = Left$(strBuff, InStr(1, strBuff, BOUNDARY))End FunctionPrivate Sub UpdataKey()
Dim strTemp$, i&, n& strTemp = EnumName("section", strINIfile)
Combo1.Clear
n = 1
Do
i = InStr(n, strTemp, vbNullChar) - 1
If (i <= 0) Then Exit Do
Me.Combo1.AddItem Mid$(strTemp, n, i)
n = i + 2
Loop
If (Combo1.ListCount > 0) Then Combo1.ListIndex = 0End Sub
Private Sub Command1_Click() WritePrivateProfileString "section", Combo1.Text, Text1.Text, strINIfile
End SubPrivate Sub Command2_Click() WritePrivateProfileString "section", Combo1.Text, 0&, strINIfile
Call UpdataKey
End SubPrivate Sub Form_Load() strINIfile = App.Path & "\Test.ini"
Call UpdataKeyEnd Sub
这段里面没有这个,当我把前面那个功能也加到这段代码里面的时候为什么老是报错啊~谁能帮我把前面Combo1的功能也加到后面的这段代码的Combo1里面?
Private Function GetItem(ByVal SectionName As String, ByVal strKey As String, ByVal INI_FileName As String) As String
Dim strBuff As String * 512
Call GetPrivateProfileString(SectionName, strKey, "", strBuff, 500, INI_FileName)
GetItem = Left$(strBuff, InStr(1, strBuff, BOUNDARY))
End FunctionPrivate Sub Combo1_Click() Text1.Text = GetItem("section", Combo1.Text, strINIfile)End Sub
GetItem = Left(strBuff, InStr(strBuff, chr(0)))