File Handling in VB.Net


This slideshow requires JavaScript.

don’t worry go through this file handling tutorial

if you want code click here

i’m giving solution visual studio file at the end of tutorial.

IN VB.net to use any file either to write or read you have to use objects that will eithrt be streamreader or streamwriter, lets stick with these to only for now.

Lets write program for basic file handling operations such as reading,writing ,deleting moving,copying and appending file.

READ

lets after clicking button1 you have to read file

     copy thsi code in in button1_click handler

in design after double-clicking read button pase this code

Dim f_name = “c:\” + Filename_box.Text + “.txt”

        Dim readobj As New System.IO.StreamReader(f_name)

        Dim combo As String

        combo = ComboBox1.Text

        If combo = “Read line by line” Then

            MsgBox(readobj.ReadLine)

        ElseIf combo = “read to end” Then

            RichTextBox1.Text = readobj.ReadToEnd()

   End If

        readobj.Close()

in f_name you are storing name of file

that wil be input of user in textbox  Filename_box

 Dim readobj As New System.IO.StreamReader(f_name)

here you are defining object readobj as reader for file f_name

now you can read content line by line or till END

i have used combo box to let user choose the option

take input in combo variable and then content of richtextbox1.text-readobj.ReadToEnd() or readobj.ReadLine

in the same way you have to write other operations

simple logic is

1. declare object for operation

2.declare file to be use with object

3.perform operations

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s