<%@ Import Namespace="System.IO"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim StrWer As StreamReader
Try
StrWer = File.OpenText(Server.MapPath("MyFiles/") & "thaicreate.txt")
Do Until StrWer.EndOfStream
Me.lblText.Text = Me.lblText.Text & StrWer.ReadLine() & "<br>"
Loop
StrWer.Close()
Catch ex As Exception
Me.lblText.Text = "Read failed. ("& ex.Message &")"
End Try
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Read Text Files</title>
</head>
<body>
<form runat="server">
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aaa As Queue
Dim lc As Integer = 0
Dim strFilePath As String
Dim aa As String
strFilePath = Application.StartupPath + "\test.txt"
If Not File.Exists(strFilePath) Then
MsgBox("File: " & strFilePath & _
" hasn't been downloaded yet. Preprocessing is being aborted.", _
MsgBoxStyle.OkOnly, "File Does Not Exist")
End If
Try
Dim sr As StreamReader = New StreamReader(strFilePath)
While Not IsNothing(sr.ReadLine)
lc += 1
End While
MessageBox.Show(lc)
sr.Close()
Catch
MsgBox("File: An error occurred while reading " & strFilePath & _
". Preprocessing is being aborted.", MsgBoxStyle.OkOnly, "File Read Error")
lc = -1
End Try
End Sub
'จาก
While Not IsNothing(sr.ReadLine)
lc += 1
End While
'เป็น
Dim LineStr As String = sr.ReadLine()
While LineStr Is Not Nothing
If (LineStr.Trim().Length > 0 ) Then
lc += 1
End If
LineStr = sr.ReadLine()
End While