
VB的问题,问一下读取ini的问题 高分
代码必须能完成以下,在1.2 中选一个写 都写也行 非常感谢
可以根据我说的意思写这个
我写了一个1.ini 文件 在/本地目录下 ini 第一行内容是 app.path 的结果 假如是C:/工程1.exe 第二行是版本 V3.5 我要求读取 第一行到 text1.text 第二行到text2.text
当然您也可以写着一个
同样是 1.ini 第一行是 C:/ 工程1.exe 第二行是 V3.5
写一个代码,要求为 能指定读取文件的行数到text1.text
OK 就这样吧 展开
你的这个INI其实只能算是一个自定义格式的数据文件,跟标准的INI文件格式是不同的。
建议你了解一下标准INI格式文件,百度百科里就有。
微软提供了一系列的INI操作函数,可以直接利用。
Option Explicit
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
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
Public ErrorMsg As String
Private Sub Class_Initialize()
ErrorMsg = vbNullString
End Sub
'写入----------------------------------
Public Function WriteString(iniFileName As String, Section As String, key As String, Value As String) As Boolean
WriteString = False
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, iniFileName) = 0 Then
ErrorMsg = "Failed to write to the ini file!"
Exit Function
End If
WriteString = True
End Function
'读出字符串----------------------------
Public Function ReadString(iniFileName As String, Section As String, key As String, Size As Long) As String
Dim ReturnStr As String
Dim ReturnLng As Long
ErrorMsg = vbNullString
ReadString = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnStr = Space(Size)
ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, iniFileName)
ReadString = Left(ReturnStr, ReturnLng)
End Function
'读出数值-----------------------------
Public Function ReadInt(iniFileName As String, Section As String, key As String) As Long
Dim ReturnLng As Long
ReadInt = 0
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnLng = GetPrivateProfileInt(Section, key, 0, iniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, iniFileName)
If ReturnLng = 1 Then
ErrorMsg = "Can not read the ini file!"
Exit Function
End If
End If
ReadInt = ReturnLng
End Function
上面是通用的INI操作函数,把它们当做储备资料吧。经常会用到的。
麻烦您 问下标准的和我这个有什么不一样?不都是ini吗
这是 INI 的百度百科说明: http://baike.baidu.com/view/509647.htm
注意里面有关格式的部分。你也许会觉得它的格式定义完全是冗余,用不着这么复杂。但当你遇到上千行的INI时就会为此感到庆幸了。
好吧,就算不考虑这些,就为了“微软已经做好了操作INI的各种接口,我们只需要拿来用”这一点,也是值得去“省事”的。
Dim strConfigPath As String '定义 ini文件的路径
Private Sub Form_Load()
Dim strTemp As String
strConfigPath = App.Path & "\config.ini" '先给路径赋值
If Dir(strConfigPath) = "" Then '检查ini文件是否存在,不存在时返回空
Open strConfigPath For Output As #1 '若没有这个文件,先建立一个ini文件
Print #1, App.Path '初始化ini文件的内容
Print #1, "版本 V3.5"
Close '用open打开后用完都要关闭
End If
Open strConfigPath For Input As #1 '打开ini文件,读取内容
Input #1, strTemp '读出第一行的值
Text1 = strTemp '将第一行的值赋给text1
Input #1, strTemp '读出第二行的值
Text2.Text = strTemp '将第二行的值赋给text1
Close
End Sub
饿 大哥谢谢 我以为没人回答,所以我就自己写了一个 读取行数 到text1.text 和写入的 不过还是谢谢你 我给分
不是你要的吗? 有什么问题还可以追问.
我自己用VB写的软件也常常用ini文件来保存设置信息的.
我也是在上班,没有什么事,就上知道来看看,顺便帮一下一些好学的初学者,还以为你是真不会呢