DotNetWebTutor.com                                                    Link to Us

Files and Streams
files and streams
A flat file is the simplest way to store data

A stream is a flow of digital information

If you want to read a file, open a stream and get the data
If you want to write a file, open a stream and send data

.Net uses the System.IO namespace to work with files
You need to import this namespace when working with files or streams

Classes:

  • File
  • FileInfo
  • Directory
  • DirectoryInfo

 

File and Directory have shared methods and cannot be instantiated
FileInfo and DirectoryInfo must be instantiated

Directory has 2 popular methods used to get a listing of files or directories:
getFiles()
getDirectories()
example

Example (Retrieve all the files in the C:\ directory)
Start a new project
Add this to the top of your code:
Add a listbox
Add the following code:

Imports system.IO
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim files() As String = Directory.GetFiles("C:\")
For Each filename As String In files
Dim file As New FileInfo(filename)
ListBox1.Items.Add(file.Name)
Next
End Sub
End Class
files in listbox

 

streamwriter, streamreader
Streamwriter, Streamreader
Both require you add the following to the top of your code:
Imports System.IO

Lets a program read and write data in a stream
StreamReader
system.io.streamreader
StreamReader has methods for reading lines, characters or blocks of characters from a stream

Common methods of the streamreader:

  • Close
    • Closes the streamreader object and released the system resources it was using
      • Ex:
      • objStreamReader.Close()
  • Readline
    • Reads a line of text from the stream.
    • A line of text is comprised of a string of characters ending in a fine feed
      • Ex:
      • Textbox1.text=objStreamReader.Readline()
  • Readtoend
    • You can use ReadToEnd to get any of the data in the stream that has yet to be read
      • Ex:
      • Textbox1.text= objStreamReader.ReadToEnd()
    • You will get an outOfMemoryException is there is too much text for your memory to handle. It is good to use a try/catch block when doing this

StreamWriter
System.io.streamwriter
The streamwriter is used to write characters to a stream.
Methods:

  • Close
    • Closes the streamWriter object and released the system resources it was using
      • Ex:
      • objStreamWriterr.Close()
  • Write
    • Writes text to the stream but does not add a line feed
      • Ex:
      • objStreamWriter.Write(“Hello”)
  • WriteLine
    • Writes a line of text from the stream.
      • Ex:
      • objStreamWriteLine(textbox1.text)

You can create a new file when you instantiate the StreamWriter:
Dim objStreamwriter As New StreamWriter("C:\test.txt")
objStreamwriter.WriteLine(textbox1.text)

Example:

  • Start a new project
  • Add a label and a button
  • Add this code to the button click event

'writing to the file
Dim myStream As New StreamWriter("C:\test.txt")
myStream.Write("Some text")
myStream.WriteLine("Write a line")
myStream.Close()

'reading from the file
Dim myReader As New StreamReader("C:\test.txt")
Label1.Text = myReader.ReadToEnd()
myReader.Close()

  • Test it
  • Check your hard drive to see if it wrote your file

notepad

files in vb