控制台下 vb 怎么读取 txt 文件啊
好像
'Dim ss As String
'Dim strFile As String
' ss = string(20, 0)
'strFile = "文件名"
' Open strFile For Binary As #1
' Get #1, , ss
'MsgBox(ss)
' Close #1
这个代码不行
...全文
19315打赏收藏
vb 读取文件
控制台下 vb 怎么读取 txt 文件啊 好像 'Dim ss As String 'Dim strFile As String ' ss = string(20, 0) 'strFile = "文件名" ' Open strFile For Binary As #1 ' Get #1, , ss 'MsgBox(ss) ' Close #1 这个代码不行
读取指定文件
'
'读取指定文本文件
Public Function readtext(ByVal path As String)
If path = "" Then
readtext = "操作失败!"
Exit Function
End If
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, FileMode.Open)
Dim sr As New StreamReader(fs)
Dim str As String
str = sr.ReadToEnd.ToString
sr.Close()
fs.Close()
readtext = str
Else
readtext = "操作失败!"
End If
Catch ex As Exception
readtext = "操作失败!"
End Try
End Function
'向指定文件写入数据
Public Function writetext(ByVal path As String, ByVal opi As Integer, ByVal msg As String)
If path = "" Then
writetext = "操作失败!"
Exit Function
End If
Dim op As FileMode
Select Case opi
Case 1
op = FileMode.Append
Case 2
op = FileMode.Create
Case Else
op = FileMode.Create
End Select
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, op)
Dim sr As New StreamWriter(fs)
sr.WriteLine(msg)
sr.Close()
fs.Close()
writetext = "操作完成!"
Else
writetext = "操作失败!"
End If
Catch ex As Exception
writetext = "操作失败!"
End Try
End Function
error BC30815: 'open' is not declared. File I/O functionality is available in the 'Microsoft.VisualBasic' namespace.
E:\Project\VB\ConsoleApplication1\ConsoleApplication1\Module1.vb(56) : error BC30815: 'close' is not declared. File I/O functionality is available in the 'Microsoft.VisualBasic' namespace.
========== Build: 0 succeeded or up-to-
E:\Project\VB\ConsoleApplication1\ConsoleApplication1\Module1.vb(39) : error BC30002: Type 'FileSystem.Object' is not defined.
E:\Project\VB\ConsoleApplication1\ConsoleApplication1\Module1.vb(40) : error BC30002: Type 'TextStream' is not defined.
可以用FileSystemObject :
dim fso as new FileSystem.Object
dim ts as TextStream
dim theURL as String
set ts = fso.OpenTextFile(filename, 1)
'这句话返回一个TextStream对象
theURL = ts.ReadAll() '读取文件所有内容
或者
theURL = ts.ReadLine() '读取一行
========================
也可以用Open语句,这个是vb自己的语句
dim theURL as String
Open filename for input as #1
Line input #1, theURL
=====================
你喜欢怎么样就怎么样,如果用FileSystemObject
需要在vb的工程中加入引用,功能简单的话,
用Open语句就行。