15-05-2009, 03:41 PM
crackerviet_87
Thành Viên SVIT
Được cảm ơn 32 trong 8 bài viết
Đã viết 42 bài thảo luận
6327 lượt xem
#1
các cách mở file và đọc file trong vb.net
mình xin đưa ra 2 cách mở file và đọc file trong vb.net .
bạn nào quan tâm thì góp ý nhé.
các bạn thêm vào project các đối tượng sau: một textbox ,listbox và 3 command.
sau đó thực hiện theo các cách sau:
Code:
Imports System.IO
PublicClass Form1
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'openfile
Dim sfile AsString
With OpenFileDialog1
.Filter = "text files (*.xml)|*.xml"
.ShowDialog()
If Len(.FileName) = 0 Then
ExitSub
EndIf
sfile = .FileName
EndWith
FileOpen(1, sfile, OpenMode.Input)
TextBox1.Text = InputString(1, CInt(LOF(1)))
FileClose(1)
EndSub
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'code openfile level 2
Dim sr As StreamReader
ListBox1.Items.Clear()
sr = New StreamReader("d:\" & "nguyenvantuan.xml")
Dim x AsString
While sr.Peek <> -1
x = sr.ReadLine()
ListBox1.Items.Add(x)
EndWhile
sr.Close()
EndSub
PrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'delete file
Dim deletefile AsString
deletefile = "d:\nguyenvantuan.xml"
If System.IO.File.Exists(deletefile) = TrueThen
System.IO.File.Delete(deletefile)
MsgBox("delefile")
EndIf
EndSub
EndClass
Chủ đề mới cùng chuyên mục
29-03-2010, 11:25 PM
Mr Đàm
Thành Viên SVIT
Được cảm ơn 5 trong 4 bài viết
Đã viết 132 bài thảo luận
#2
các cách mở file và đọc file trong vb.net
Bạn ơi giúp mình chỗ With OpenFileDialog1
.Filter = "text files (*.xml)|*.xml"
.ShowDialog()
Nó báo ShowDialog() is not member a string
www.mrdamniit.tk
04-06-2012, 07:38 PM
bboy_nonoyes
Thành Viên SVIT
Được cảm ơn 268 trong 154 bài viết
Đã viết 1,011 bài thảo luận
#3
các cách mở file và đọc file trong vb.net
Theo mình thì các bạn nên dùng cách 2, tức là dùng StreamReader & StreamWriter để đọc & ghi tập tin.
Chủ topic đã có ví dụ về StreamReader, mình xin đc bổ sung thêm ví dụ về StreamWriter đơn giản như sau:
Code:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New StreamWriter("C:\SVIT.txt", True)
f.WriteLine("Chào mừng các bạn đến với SinhVienIT.net!")
f.Close()
MsgBox("Đã ghi vào tập tin tại C:\SVIT.txt", MsgBoxStyle.OkOnly, "Thông báo")
End Sub
End Class
Chúc các bạn thành công!
Nhấn "Cảm ơn" khi đọc được bài viết hữu ích chứng tỏ bạn là người lịch sự!