ReadWriteFileProgram 020955

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Read/Write File program

Imports System.IO
Imports System.Text
Imports System.IO.StreamReader

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
If File.Exists(FolderBrowserDialog1.SelectedPath & "\" &
TextBox1.Text) Then
MsgBox("File already exist!!!")
Else
Dim wfile As FileStream
Dim data() As Byte
data = Encoding.ASCII.GetBytes(TextBox2.Text)
wfile = New FileStream(FolderBrowserDialog1.SelectedPath & "\" &
TextBox1.Text, FileMode.Append)
wfile.Write(data, 0, data.Length)
wfile.Close()
MsgBox("File successfully created")
End If
TextBox1.Clear()
TextBox2.Clear()
Catch ex As Exception
MsgBox("Error while creating the file")
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
OpenFileDialog1.DefaultExt = "TXT"
OpenFileDialog1.DefaultExt = "TXT"
OpenFileDialog1.Filter = "Text Files|*.TXT|HTML Files|*.HTM|All Files|*.*"
OpenFileDialog1.FilterIndex = 1

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then


Try
If Not (File.Exists(OpenFileDialog1.FileName)) Then
MsgBox("File could not be traced!!!")
Else
Dim rfile As StreamReader
rfile = New StreamReader(OpenFileDialog1.FileName)
TextBox3.Text = rfile.ReadToEnd
rfile.Close()
rfile = Nothing
End If

Catch ex As Exception
MsgBox("Error while opening the file")
End Try
End If
End Sub
End Class

You might also like