vb.net虽也有input语句,但一次只能读取到一个变量中,可以用TextFieldParser类代替,但似乎没以前的方便。不过比以前的更灵活。写入文件Write还是可以用,在Microsoft.VisualBasic.FileIO中。
成都创新互联公司专业为企业提供伊宁网站建设、伊宁做网站、伊宁网站设计、伊宁网站制作等企业网站建设、网页设计与制作、伊宁企业网站模板建站服务,10年伊宁做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileName As String = "E:\User Documents\Master\My Documents\电子阅读\股票\table2.csv"
Using Recrods As New Microsoft.VisualBasic.FileIO.TextFieldParser(fileName) '建立TextFieldParser对象
'MyReader.TextFieldType = FieldType.Delimited
Recrods.SetDelimiters(",") '把字段分隔符设置为","
Dim curRow() As String
Do Until Recrods.EndOfData
curRow = Recrods.ReadFields() '读取记录行,返回字符串数组,所以不同字段类型需要自己转换。
Debug.Print(Join(curRow, vbTab))
Loop
End Using
End Sub
如果想继续编辑之前的文档,在TXT文件尾部继续添加文本,那么还需要在函数后边加个参数。
VB 代码
方法1:
Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt")
sw.Write("abc" vbCrLf)
sw.Close()
Dim sw2 As StreamWriter = New StreamWriter("C:\temp\test.txt", True)
sw2.Write("456" vbCrLf)
sw2.Close()
方法2:
My.Computer.FileSystem.WriteAllText("test.txt", "This is test Text", True)
方法3:
System.IO.File.AppendAllText("c:\temp\test.txt", "this is extra test file")
详见:“网页链接”
Private Sub Cmddel_Click()
dim i as integer,FN as string,Fns() as string
fns=split("A.dat、B.exe、C.mp3、D.mp3","、")
for i=0 to ubound(fns)
fn=syspath "\" fns(i)
if dir(fn)"" then kill fn
next
End Sub
VB.NET打开二进制文件用fileopen完成,打开二进制文件的形式为:openmode.binary
读取二进制文件用的是fileget方法,写入二进制文件用的是fileput方法。
应用示例:将一批随机数保存在一个dat文件中,然后再将其提取到文本框中。
二进制文件的读写一批随机数的存取,程序为:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, i, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
For i = 1 To 8
x = Int(Rnd() * 100)
s = s + Str(x)
FilePut(fn, x)
Next
FileClose(fn)
TextBox1.Text = s
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
Do While Not EOF(fn)
FileGet(fn, x)
s = s + Str(x) + " "
Loop
FileClose(fn)
TextBox1.Text = s
End Sub