VB 读指定行
http://zhidao.baidu.com/question/81532606.html
VB 读指定行
悬赏分:10 - 解决时间:2009-1-20 11:41
vb中如何读取指定行的内容,窗体上有两个标签,文件在C:\A.TXT,四个按钮,单击第一个,读文件的第一行到标签一,第二行到标签二;单击第二个按钮,第三行到标签一,第四行到标签二;以此类推,谢谢
提问者: imyangmo - 书生 二级
最佳答案
用文件系统可以读取指定行。。。但是如果文件内容不是很多的话用顺序文件全部读出来截断到数组后提取指定行更方便。。。
使用文件系统读取指定行:
首先需要“引用”Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim FSO As New FileSystemObject
Dim TS As TextStream
Dim Str As String
If Dir("C:\A.txt") <> "" Then '判断文件是否存在
Set TS = FSO.OpenTextFile("C:\A.txt", ForReading) 'ForReading为读模式,另外有写和追加模式
Do Until TS.Line = 1 '跳到指定行时终止循环
TS.SkipLine '跳过一行
Loop
Str = TS.ReadLine '读出行,文件系统读写位置就是光标所在的位置
Print Str
Else
MsgBox "文件不存在!"
End If
'但是文件系统好像不能写指定行
End Sub
----------------------------------------------------------------
使用顺序文件获取指定行内容
Private Sub Command1_Click()
Dim Arr
Dim Str As String
Dim i As Integer
Open "C:\A.txt" For Binary As #1
Str = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
Arr = Split(Str, vbCrLf)
For i = 0 To UBound(Arr)
Print Arr(i)
Next i
End Sub
相关文档:
Option Explicit
Dim FSO As New FileSystemObject
Dim currentFolder As Folder
Dim currentFolderName As String
Dim folderItem, fileItem
Private Sub Form_Load()
currentFolderName = FSO.GetFolder(App.Path) & "\content"
SeachFolder (FSO.GetFolder(currentFolderName))
End Sub ......
今天在公司服务器上看一个老掉牙的asp程序,发现竟然调用了自定义com组件,是vb写的,封装了数据库连接与操作,文档上写这样做是为什么用户到服务器也看不到数据库的用户名与密码,也对啊,这个是财务的服务器,当然不能让我们it随便看了,呵呵!(it忽悠finance,让finance相信it看不到数据库用户名与密码,my god)由于, ......
Private Sub Command1_Click()
Dim MyComm As New ADODB.Command '定义一个命令对象
Dim Rs_GetList As New ADODB.Recordset '定义一个记录集对象
Dim param As ADODB.Parameter ......
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
&nbs ......
'添加个picture1和command1
Option Explicit
Dim lngCenter As Long
Dim lngMax As Long
Dim lngPad&
Dim PCurrent As POINT, PLast As POINT
Private Type POINT
x As Long
y As Long
End Type
Private Sub Form_Load()
lngCenter = (Picture1.Top + Picture1.Height) / 2
lngMax = Pictu ......