vb 读取文件

yuio134459 2008-10-27 02:31:28
控制台下 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
这个代码不行 
...全文
193 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
10楼不就是我,晕~~~~~~~~~~~~
yuio134459 2008-10-27
  • 打赏
  • 举报
回复
看10楼的
yuio134459 2008-10-27
  • 打赏
  • 举报
回复
不是给你分了啊
  • 打赏
  • 举报
回复
LZ信誉太差了,鄙视你
  • 打赏
  • 举报
回复
可以给我加分了吧
  • 打赏
  • 举报
回复
原来是VB.NET啊,那就用流来操作吧

读取指定文件
'
'读取指定文本文件
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

参考这个吧
yuio134459 2008-10-27
  • 打赏
  • 举报
回复
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-
vbman2003 2008-10-27
  • 打赏
  • 举报
回复
dim h as long
dim sFile as string
dim s as string

h=freefile
open sFile for binary as h
s=space(lof(h))
get #h,,s
close

msgbox s
yuio134459 2008-10-27
  • 打赏
  • 举报
回复
图片 就是 我添加的 那个 Microsoft Scripting Runtime
yuio134459 2008-10-27
  • 打赏
  • 举报
回复
不行 还是提示没有定义


是不是我用的编译器 有问题啊 我用的是 vs2008 

而且我想用 Open 为什么我 打进去 只有  OpenAccess()
OpenMode OpenShare
这三个函数
yuio134459 2008-10-27
  • 打赏
  • 举报
回复
谢谢 大哥 我 再试试
刚刚找了好久 没找到哦
  • 打赏
  • 举报
回复
单击“工程”,“引用”,然后在“引用”对话框中选中“Microsoft Scripting Runtime”前的复选框,然后单击“确定”
  • 打赏
  • 举报
回复
你没在工程中引用FileSystemObject当然说你没定义了
yuio134459 2008-10-27
  • 打赏
  • 举报
回复
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语句就行。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧