KEMBAR78
File Read/Write Program in VB.NET | PDF
0% found this document useful (0 votes)
75 views2 pages

File Read/Write Program in VB.NET

This document describes a program that allows a user to read and write files. It contains code to: 1) Allow the user to select a folder and file name to write text to using a file stream. 2) Allow the user to select an existing file to open and read the entire contents into a text box. 3) Display error messages if the file cannot be created, opened, or found.

Uploaded by

Sam Khar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views2 pages

File Read/Write Program in VB.NET

This document describes a program that allows a user to read and write files. It contains code to: 1) Allow the user to select a folder and file name to write text to using a file stream. 2) Allow the user to select an existing file to open and read the entire contents into a text box. 3) Display error messages if the file cannot be created, opened, or found.

Uploaded by

Sam Khar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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