Imports System.IO
Public Class F16CountLine
'สร้างฟอร์มแล้วสร้าง Controls 3 ตัว
'1.Button1
'2.Label1
'3.Label2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileD As New OpenFileDialog
With FileD
.Multiselect = False
.Filter = "TextFile|*.vb;*.cs;*.txt|All Files|*.*"
.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
.Title = "เลือกไฟล์ที่ต้องการนับบรรทัด"
End With
If FileD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fs As New FileStream(FileD.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim sr As New StreamReader(fs)
Dim CountLine As Integer
While sr.Peek() > -1
Dim strLine As String = sr.ReadLine()
If strLine Is Nothing OrElse strLine.Trim = "" Then
ElseIf strLine.Trim.StartsWith("'") Or strLine.Trim.StartsWith("//") Then
Else
CountLine += 1
End If
End While
Me.Label1.Text = "ชื่อแฟ้ม = " & FileD.FileName
Me.Label2.Text = "จำนวนบรรทัดที่นับได้ = " & CountLine & " บรรทัด"
End If
End Sub
End Class