HI GUYS, NOW I WILL SHARE A SIMPLE CODES ON HOW TO WRITE AND READ FILE USING VB.NET.
TOOLS:
3 TEXTBOX
2 BUTTONS
CODES:
Imports System.Text
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'THIS CODES FOR MAKING A NOTEPAD
Dim fs As FileStream = New FileStream(txtTitle.Text + ".log", FileMode.Append, FileAccess.Write)
'THIS CODES TO WRITE IF THE FILESTREAM CAN WRITE
If fs.CanWrite Then
Dim write As Byte() = System.Text.Encoding.ASCII.GetBytes(txtWrite.Text + Environment.NewLine)
fs.Write(write, 0, write.Length)
End If
fs.Flush()
fs.Close()
MessageBox.Show("The File has Save In Same Folder Of Your Application Sucessfully!")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'THIS CODES FOR RETRIEVING THE FILE IF EXIST
Using sr As StreamReader = File.OpenText(Path.Combine(txtTitle.Text + ".log"))
Dim i As Integer = 0
While sr.Peek() >= 0
If i Mod 2 = 0 Then
'THEN IF THE FILE IS EXIST,PASS INTO THE TXTREAD TEXTBOX
txtRead.AppendText(sr.ReadLine() & vbCrLf)
Else
txtRead.AppendText(sr.ReadLine() & vbCrLf)
End If
i += 1
End While
End Using
End Sub
End Class
No comments:
Post a Comment