VB.NET读取和写入文本文件

inseg2 2008-05-22 10:31:12
我已经用OpenFileDialog选取文件,并获得路径
可是VB.NET应该如何读取该文件,文本文件里的内容是二进制。
...全文
268 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pang2042 2008-05-26
  • 打赏
  • 举报
回复
你别管是什么文件,只要这个文件的内容是二进制的,你就是binaryreader这个类来读取其内容.关于这个类的具体操作你在MSDN上查就是了.下面我给你抄一个示例
Imports System
Imports System.IO

Public Class BinaryRW

Shared Sub Main()

Dim i As Integer = 0
Dim invalidPathChars() As Char = Path.InvalidPathChars
Dim memStream As new MemoryStream()
Dim binWriter As New BinaryWriter(memStream)

' Write to memory.
binWriter.Write("Invalid file path characters are: ")
For i = 0 To invalidPathChars.Length - 1
binWriter.Write(invalidPathChars(i))
Next i

' Create the reader using the same MemoryStream
' as used with the writer.
Dim binReader As New BinaryReader(memStream)

' Set Position to the beginning of the stream.
memStream.Position = 0

' Read the data from memory and write it to the console.
Console.Write(binReader.ReadString())
Dim memoryData( _
CInt(memStream.Length - memStream.Position) - 1) As Char
For i = 0 To memoryData.Length - 1
memoryData(i) = Convert.ToChar(binReader.Read())
Next i
Console.WriteLine(memoryData)

End Sub
End Class

Roy 2008-05-26
  • 打赏
  • 举报
回复
我这里有一个写入文件的例子,希望对你有帮助,如下所示:
其中XML为写入的内容,fp为写入的文件,即会先判断该文件是否存在,若存在会先Kill掉哦。


Imports System.IO

Private Function gf_WriteFile(ByVal xml As String, ByVal fp As String) As Boolean

Try
'Kill(fp)
If File.Exists(fp) = True Then Kill(fp)

Dim fs As New FileStream(fp, FileMode.OpenOrCreate)
Dim wr As New StreamWriter(fs)
Dim str As String
wr.Write(xml)
wr.Close()
fs.Close()

Return True
Catch ex As Exception
Return False
End Try
End Function
hzybc 2008-05-23
  • 打赏
  • 举报
回复
'写文件
fileopen(1,"c:\y.txt",openmode.output)
printline(1,"我")
printline(1,"是")
printline(1,"一")
printline(1,"个")
printline(1,"兵")
fileclose(1)
'读文件
fileopen(1,"c:\y.txt",openmode.input)
textbox1.text= lineinput(1)
...
fileclose(1)
horizonlin 2008-05-22
  • 打赏
  • 举报
回复
openfiledialog只能帮你取得文件路径和文件名等,要打开文件还得用另外的System.IO.File
这个
HimeTale 2008-05-22
  • 打赏
  • 举报
回复
Dim strContent As String = String.Empty 
Dim sr As New StreamReader("c:\111.txt", Encoding.[Default])
'strContent = sr.ReadToEnd;
While Not sr.EndOfStream
strContent += sr.ReadLine() + Environment.NewLine
End While
yanlongwuhui 2008-05-22
  • 打赏
  • 举报
回复
Dim strFilePath_R As String
Dim intFileNum_R As Integer
Dim bAsc() As Byte
Dim intFileLength As Integer

strFilePath_R = Application.StartupPath & "\工程1.log"
intFileNum_R = FreeFile()
FileOpen(intFileNum_R, strFilePath_R, OpenMode.Binary, OpenAccess.Read)
intFileLength = LOF(intFileNum_R)
ReDim bAsc(intFileLength - 1)
FileGet(intFileNum_R, bAsc)
FileClose(intFileNum_R)

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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